如何使用web2py和python-requests对全部数据进行GET请求响应 [英] How to response with full range of data to GET request using web2py and python-requests

查看:201
本文介绍了如何使用web2py和python-requests对全部数据进行GET请求响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对于 web2py 以及网络请求都很陌生,因此请保持松懈。我尝试使用 web2py 框架进行应用程序,该框架允许我执行以下操作:
将POST请求发送到远程服务器(例如,服务器的url为 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(有效内容),headers =标题)

服务器接收到请求并使用计数器GET请求尝试从位于我的 some_file.json 中获取数据硬盘在 / home / user / Desktop / some_files 中,并与 web2py 应用程序页面链接 http :// localhost:8000 / myApp / default / file_to_process 以下代码

 控制器:
def file_to_process():
return dict(files = Expose('/ home / user / Deskto
$ {$ files}}
>问题是服务器只能从文件接收第一个字符串,但不能接收整个数据范围......我无法理解我应该在哪里搜索一个错误:在 web2py 代码或 Python请求 POST请求。
请提出您的建议或提供解决方案。

解决方案

Expose 功能实际上是为了在浏览器中构建一个用于在给定目录中列出和下载文件的UI,因此它缺少一些灵活性(例如,它只是简单地向服务器返回一个打开的文件对象,而不设置Content-Length头文件,这可能导致文件通过分块传输编码方式提供,具体取决于web2py的服务方式)。

然而,你真的不需要在这里公开,因为您可以简单地使用 response.stream 来提供个别请求的文件:

  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)

请注意,如果文件名有一个.json扩展名,设置Content-Type头是不必要的,因为 response.stream 会自动处理。



另外,如果远程服务器允许它,而不是让远程服务器从web2py请求文件,那么可以考虑直接使用初始请求将文件发布到远程服务器(例如,参见 https://toolbelt.readthedocs.org/en/latest/uploading-data.html )。


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.

解决方案

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).

这篇关于如何使用web2py和python-requests对全部数据进行GET请求响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆