在Python,有什么之间的差异urllib,urllib2以及urllib3模块?为什么有三个?他们似乎做同样的事情......
我需要将JSON从客户端POST到服务器.我正在使用Python 2.7.1和simplejson.客户端正在使用请求.服务器是CherryPy.我可以从服务器获取硬编码的JSON(代码未显示),但是当我尝试将JSON发送到服务器时,我得到"400 Bad Request".
这是我的客户端代码:
data = {'sender': 'Alice',
'receiver': 'Bob',
'message': 'We did it!'}
data_json = simplejson.dumps(data)
payload = {'json_payload': data_json}
r = requests.post("http://localhost:8080", data=payload)
Run Code Online (Sandbox Code Playgroud)
这是服务器代码.
class Root(object):
def __init__(self, content):
self.content = content
print self.content # this works
exposed = True
def GET(self):
cherrypy.response.headers['Content-Type'] = 'application/json'
return simplejson.dumps(self.content)
def POST(self):
self.content = simplejson.loads(cherrypy.request.body.read())
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
每当我尝试导入时requests,我都会收到错误消息No module Named requests.
import requests
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
File "ex2.py", line 1, in <module>
import requests
ImportError: No module named requests
Run Code Online (Sandbox Code Playgroud) 请求是一个非常好的库.我想用它来下载大文件(> 1GB).问题是不可能将整个文件保存在内存中我需要以块的形式读取它.这是以下代码的问题
import requests
def DownloadFile(url)
local_filename = url.split('/')[-1]
r = requests.get(url)
f = open(local_filename, 'wb')
for chunk in r.iter_content(chunk_size=512 * 1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.close()
return
Run Code Online (Sandbox Code Playgroud)
由于某种原因它不起作用.在将其保存到文件之前,它仍会将响应加载到内存中.
UPDATE
如果你需要一个可以从FTP下载大文件的小客户端(Python 2.x /3.x),你可以在这里找到它.它支持多线程和重新连接(它确实监视连接),它还为下载任务调整套接字参数.
try:
r = requests.get(url, params={'s': thing})
except requests.ConnectionError, e:
print e #should I also sys.exit(1) after this?
Run Code Online (Sandbox Code Playgroud)
它是否正确?有没有更好的方法来构建它?这会涵盖我的所有基础吗?
默认情况下,Requests python库将日志消息写入控制台,其行如下:
Starting new HTTP connection (1): example.com
http://example.com:80 "GET / HTTP/1.1" 200 606
Run Code Online (Sandbox Code Playgroud)
我通常对这些消息不感兴趣,并希望禁用它们.什么是沉默这些消息或减少请求的冗长的最佳方法?
我正在尝试使用python的requests模块从Web下载并保存图像.
这是我使用的(工作)代码:
img = urllib2.urlopen(settings.STATICMAP_URL.format(**data))
with open(path, 'w') as f:
f.write(img.read())
Run Code Online (Sandbox Code Playgroud)
以下是使用以下内容的新(非工作)代码requests:
r = requests.get(settings.STATICMAP_URL.format(**data))
if r.status_code == 200:
img = r.raw.read()
with open(path, 'w') as f:
f.write(img)
Run Code Online (Sandbox Code Playgroud)
你能帮助我从响应中使用什么属性requests吗?
我正在编写一个涉及CAS,jspring安全检查,重定向等的简单脚本.我想使用Kenneth Reitz的python请求,因为它是一项很棒的工作!但是,CAS需要通过SSL进行验证,因此我必须先通过该步骤.我不知道Python的要求是什么?这个SSL证书应该驻留在哪里?
Traceback (most recent call last):
File "./test.py", line 24, in <module>
response = requests.get(url1, headers=headers)
File "build/bdist.linux-x86_64/egg/requests/api.py", line 52, in get
File "build/bdist.linux-x86_64/egg/requests/api.py", line 40, in request
File "build/bdist.linux-x86_64/egg/requests/sessions.py", line 209, in request
File "build/bdist.linux-x86_64/egg/requests/models.py", line 624, in send
File "build/bdist.linux-x86_64/egg/requests/models.py", line 300, in _build_response
File "build/bdist.linux-x86_64/egg/requests/models.py", line 611, in send
requests.exceptions.SSLError: [Errno 1] _ssl.c:503: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
Run Code Online (Sandbox Code Playgroud) 就我而言,我正在使用该requests库通过HTTPS调用PayPal的API.不幸的是,我从PayPal收到错误,并且PayPal支持无法弄清楚错误是什么或导致错误.他们要我"请提供整个请求,包括标题".
我怎样才能做到这一点?
我使用Python 2.7.3和请求.我通过pip安装了Requests.我相信这是最新版本.我正在使用Debian Wheezy.
我过去曾经多次使用过Requests并且从未遇到过这个问题,但似乎在Requests我发出https请求时会遇到InsecurePlatform异常.
错误提到urllib3,但我没有安装.我确实安装它以检查它是否解决了错误,但事实并非如此.
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3
/util/ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not
available. This prevents urllib3 from configuring SSL appropriately and
may cause certain SSL connections to fail. For more information, see
https://urllib3.readthedocs.org/en/latest
/security.html#insecureplatformwarning.
Run Code Online (Sandbox Code Playgroud)
关于为什么我得到这个的任何想法?我已经检查了错误消息中指定的文档,但文档说要导入urllib3并禁用警告或提供证书.