I am running Tornado with stream_request_body
and saving a file POST
request to file on server. In prepare
I create the file object and in post
I close
file object. How do I capture any error/exception in data_received
so that I can close
the file object properly?
JavaScript
x
29
29
1
from tornado.ioloop import IOLoop
2
from tornado.web import Application, RequestHandler, stream_request_body
3
4
@stream_request_body
5
class UploadHandler(RequestHandler):
6
7
def prepare(self):
8
self.file = open('uploaded_file', 'wb')
9
10
def data_received(self, chunk):
11
print(len(chunk))
12
self.file.write(chunk)
13
14
# force an exception
15
raise Exception('Error')
16
17
def post(self, *args, **kwargs):
18
self.file.close()
19
self.write('UPLOADED')
20
21
22
app = Application([(r'/upload', UploadHandler), ],
23
debug=True)
24
25
26
if __name__ == '__main__':
27
app.listen(7777)
28
IOLoop.current().start()
29
terminal output where tornado app is running
JavaScript
1
14
14
1
$ python upload.py
2
232
3
ERROR:tornado.application:Uncaught exception
4
Traceback (most recent call last):
5
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib64/python3.6/site-packages/tornado/http1connection.py", line 659, in _read_fixed_body
6
ret = delegate.data_received(body)
7
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib64/python3.6/site-packages/tornado/routing.py", line 264, in data_received
8
return self.delegate.data_received(chunk)
9
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib64/python3.6/site-packages/tornado/web.py", line 2279, in data_received
10
return self.handler.data_received(data)
11
File "upload.py", line 13, in data_received
12
raise Exception('Error')
13
Exception: Error
14
terminal output with python interpreter for the request
JavaScript
1
60
60
1
>>> resp=requests.post('http://localhost:7777/upload', files={'file': open('test.txt', 'rb')})
2
Traceback (most recent call last):
3
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/connectionpool.py", line 706, in urlopen
4
chunked=chunked,
5
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/connectionpool.py", line 445, in _make_request
6
six.raise_from(e, None)
7
File "<string>", line 3, in raise_from
8
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/connectionpool.py", line 440, in _make_request
9
httplib_response = conn.getresponse()
10
File "/usr/lib64/python3.6/http/client.py", line 1346, in getresponse
11
response.begin()
12
File "/usr/lib64/python3.6/http/client.py", line 307, in begin
13
version, status, reason = self._read_status()
14
File "/usr/lib64/python3.6/http/client.py", line 276, in _read_status
15
raise RemoteDisconnected("Remote end closed connection without"
16
http.client.RemoteDisconnected: Remote end closed connection without response
17
18
During handling of the above exception, another exception occurred:
19
20
Traceback (most recent call last):
21
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
22
timeout=timeout
23
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/connectionpool.py", line 756, in urlopen
24
method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
25
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/util/retry.py", line 532, in increment
26
raise six.reraise(type(error), error, _stacktrace)
27
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/packages/six.py", line 734, in reraise
28
raise value.with_traceback(tb)
29
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/connectionpool.py", line 706, in urlopen
30
chunked=chunked,
31
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/connectionpool.py", line 445, in _make_request
32
six.raise_from(e, None)
33
File "<string>", line 3, in raise_from
34
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/connectionpool.py", line 440, in _make_request
35
httplib_response = conn.getresponse()
36
File "/usr/lib64/python3.6/http/client.py", line 1346, in getresponse
37
response.begin()
38
File "/usr/lib64/python3.6/http/client.py", line 307, in begin
39
version, status, reason = self._read_status()
40
File "/usr/lib64/python3.6/http/client.py", line 276, in _read_status
41
raise RemoteDisconnected("Remote end closed connection without"
42
urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))
43
44
During handling of the above exception, another exception occurred:
45
46
Traceback (most recent call last):
47
File "<stdin>", line 1, in <module>
48
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/requests/api.py", line 119, in post
49
return request('post', url, data=data, json=json, **kwargs)
50
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/requests/api.py", line 61, in request
51
return session.request(method=method, url=url, **kwargs)
52
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/requests/sessions.py", line 542, in request
53
resp = self.send(prep, **send_kwargs)
54
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/requests/sessions.py", line 655, in send
55
r = adapter.send(request, **kwargs)
56
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/requests/adapters.py", line 498, in send
57
raise ConnectionError(err, request=request)
58
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))
59
60
Advertisement
Answer
RequestHandler
has an on_connection_close
method which is called when a connection is closed. You can override this to close the file object:
JavaScript
1
5
1
class UploadHandler( ):
2
3
def on_connection_close(self):
4
self.file.close()
5