草庐IT

Requests

全部标签

python - 获取 Python Requests 中状态码的描述

我希望能够输入服务器响应代码并让请求告诉我代码的含义。比如code200-->ok我找到了sourcecode的链接它显示了代码和描述的字典结构。我看到请求将返回给定描述的响应代码:printrequests.codes.processing#returns102printrequests.codes.ok#returns200printrequests.codes.not_found#returns404但不是相反:printrequests.codes[200]#returnsNoneprintrequests.codes.viewkeys()#returnsdict_keys([

Python:requests.exceptions.ConnectionError。 url 超出了最大重试次数

这是脚本:importrequestsimportjsonimporturlparsefromrequests.adaptersimportHTTPAdapters=requests.Session()s.mount('http://',HTTPAdapter(max_retries=1))withopen('proxies.txt')asproxies:forlineinproxies:proxy=json.loads(line)withopen('urls.txt')asurls:forlineinurls:url=line.rstrip()data=requests.get(ur

python - 可以在 Google App Engine 上使用 Python Requests 库吗?

我可以使用Requests在谷歌应用引擎上?我认为这个库非常适合创建REST客户端。 最佳答案 安装requests-toolbelt库:https://github.com/sigmavirus24/requests-toolbelt对于AppEngine,它可能类似于:pipinstallrequests-toolbelt-tlib(参见:https://cloud.google.com/appengine/docs/python/tools/using-libraries-python-27#installing_a_libr

python - 从 Flask 返回 requests.Response 对象

我正在尝试使用Flask和请求构建一个简单的代理。代码如下:@app.route('/es///',methods=['GET','POST','PUT']):defes(index,type,id):elasticsearch=find_out_where_elasticsearch_lives()#alsohandlesomeauthenticationurl='%s%s%s%s'%(elasticsearch,index,type,id)esreq=requests.Request(method=request.method,url=url,headers=request.hea

python - 使用 python-requests 获取文件大小,同时只获取标题

我查看了请求文档,但似乎找不到任何东西。如何只请求header,以便评估文件大小? 最佳答案 发送HEADrequest:>>>importrequests>>>response=requests.head('http://example.com')>>>response.headers{'connection':'close','content-encoding':'gzip','content-length':'606','content-type':'text/html;charset=UTF-8','date':'Fri,1

python - 如何使用 requests.post (Python) 发送数组? "Value Error: Too many values to unpack"

我正在尝试使用requests.post向WheniWorkAPI发送请求数组(列表),但我不断收到两个错误之一。当我将列表作为列表发送时,我收到一个解包错误,当我将它作为一个字符串发送时,我收到一个错误,要求我提交一个数组。我认为这与请求如何处理列表有关。以下是示例:url='https://api.wheniwork.com/2/batch'headers={"W-Token":"Ilovemyboss"}data=[{'url':'/rest/shifts','params':{'user_id':0,'other_stuff':'value'},'method':'post',

python - 使用 Python requests 模块下载并保存 PDF 文件

我正在尝试从网站下载PDF文件并将其保存到磁盘。我的尝试要么因编码错误而失败,要么导致PDF为空白。In[1]:importrequestsIn[2]:url='http://www.hrecos.org//images/Data/forweb/HRTVBSH.Metadata.pdf'In[3]:response=requests.get(url)In[4]:withopen('/tmp/metadata.pdf','wb')asf:...:f.write(response.text)--------------------------------------------------

python - 对使用 requests 库的 python 应用程序进行单元测试

我正在编写一个使用KennethReitz的requestslibrary执行REST操作的应用程序。我正在努力寻找一种对这些应用程序进行单元测试的好方法,因为requests通过模块级方法提供它的方法。我想要的是合成双方对话的能力;提供一系列请求断言和响应。 最佳答案 实际上有点奇怪的是,该库有一个关于最终用户单元测试的空白页面,同时以用户友好性和易用性为目标。然而,Dropbox有一个易于使用的库,不出所料地称为responses。.这是它的intropost.它说他们没有雇用httpretty,同时说明没有失败的原因,并编写了

python - 记录来自 python-requests 模块的所有请求

我正在使用pythonRequests.我需要调试一些OAuth事件,为此我希望它记录所有正在执行的请求。我可以使用ngrep获取此信息,但遗憾的是无法grephttps连接(OAuth需要这些连接)如何激活对Requests正在访问的所有URL(+参数)的记录? 最佳答案 您需要在httplib级别启用调试(requests→urllib3→httplib)。这里有一些功能可以切换(..._on()和..._off())或暂时打开它:importloggingimportcontextlibtry:fromhttp.clienti

python - 如何使用 Python Requests 库在 post 请求中发送 cookie?

我正在尝试使用Requests库以发送带有发布请求的cookie,但我不确定如何根据其文档实际设置cookie。该脚本用于维基百科,需要发送的cookie格式如下:enwiki_session=17ab96bd8ffbe8ca58a78657a918558e;path=/;domain=.wikipedia.com;HttpOnly但是,requests文档快速入门仅给出了以下示例:cookies=dict(cookies_are='working')如何使用这个库对上述cookie进行编码?我是否需要使用python的标准cookie库来制作它,然后将它与POST请求一起发送?