问题
I'm very new to web2py
and to web-requests so please keep a slack hand. I try to make application with web2py
framework that allow me to do following:
I send POST request to remote server (server's url, for example, is https://100.100.10.100
)
headers = {'Content-type': 'application/json'}
payload = {"uuid": some_file.json,
"level": "public",
"Url": " http://localhost:8000/myApp/default/file_to_process}
requests.post('https://100.100.10.100', data=json.dumps(payload), headers=headers)
Server receives request and with counter GET request tries to get data from some_file.json
that located on my hard drive in /home/user/Desktop/some_files
and linked with web2py
application's page http://localhost:8000/myApp/default/file_to_process
with below code
Controller:
def file_to_process():
return dict(files=Expose('/home/user/Desktop/some_files'))
View:
{{=files}}
The problem is that server can receive only first string from file but not the whole data range... I can't understand where should I search for a mistake: in web2py
code or in Python requests
POST request.
Please make your suggestions or provide a solution.
回答1:
The Expose
functionality is really intended to enable building a UI in the browser for listing and downloading files in a given directory, and so it lacks some flexibility (e.g., it simply returns an open file object to the server without setting the Content-Length header, which can result in the file being served via chunked transfer encoding depending on how web2py is being served).
However, you really don't need Expose
here, as you can simply use response.stream
to serve individually requested files:
import os
def file_to_process():
path = os.path.join('home', 'user', 'Desktop', 'some_files', *request.args)
response.headers['Content-Type'] = 'application/json'
return response.stream(path)
Note, if the filenames have a .json extension, setting the Content-Type header is unnecessary, as response.stream
will handle that automatically.
Also, if the remote server will allow it, rather than having the remote server request the files from web2py, you might consider just posting the files directly to the remote server with the initial request (e.g., see https://toolbelt.readthedocs.org/en/latest/uploading-data.html).
来源:https://stackoverflow.com/questions/31854445/how-to-response-with-full-range-of-data-to-get-request-using-web2py-and-python-r