Skip to content

Commit ebe3cb3

Browse files
committed
Safe from Timeout, Works Fine for Javatpoint
1 parent 2a6d4f9 commit ebe3cb3

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

app.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ def input_post():
2828
@app.route('/<outFile>')
2929
def getDownload(outFile) :
3030
try:
31-
files = [f for f in os.listdir('.') if os.path.isfile(f)]
31+
files = [str(f) for f in os.listdir('.') if os.path.isfile(f)]
3232
for f in files:
3333
print(f)
34-
return send_file(filename_or_fp = str(outFile + '.pdf'), as_attachment=True), None
34+
if str(outFile + '.pdf') in files:
35+
return send_file(filename_or_fp = str(outFile + '.pdf'), as_attachment=True), None
36+
return send_file(filename_or_fp = str(outFile + '.html'), as_attachment=True), None
3537
except Exception as E:
3638
print(E)
3739
return 'Error'
3840

3941
if __name__ == '__main__':
40-
app.run(host="localhost", port=8000, debug=True)
42+
app.run(host="localhost", port=8000, debug=False)

main.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from urllib import request
22
from bs4 import BeautifulSoup
3+
from multiprocessing import Process
34
import json
45
import os
56
import sys
67
import pdfkit
78
import subprocess
89
import platform
910
import re
11+
import time
12+
1013

1114
def getPDF(filename = 'out'):
1215

@@ -170,7 +173,11 @@ def getAbsolute(self, content, domain):
170173
def util(self, URL, iterations = 1, filename = 'out'):
171174
print(str(iterations) + " pages to go . . .")
172175
if iterations < 1:
173-
return getPDF(filename)
176+
action_process = Process(target=lambda : getPDF(filename))
177+
action_process.start()
178+
action_process.join(timeout=15)
179+
action_process.terminate()
180+
return None
174181
req = request.urlopen(URL)
175182
html = req.read().decode('utf-8')
176183
domain = self.domain

0 commit comments

Comments
 (0)