标签: python-requests

在python中使用请求时无法获取本地颁发者证书

这是我的代码

import requests;
url='that website';
headers={
  'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
  'Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8,ja;q=0.7',
  'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
};
r = requests.get(url,headers=headers);
print(r);
print(r.status_code);
Run Code Online (Sandbox Code Playgroud)

然后它遇到了这个:

requests.exceptions.SSLError:

HTTPSConnectionPool(host='www.xxxxxx.com', port=44 3):

url: xxxxxxxx 超出最大重试次数(由 SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED]

证书验证失败:无法获得本地颁发者证书 (_ssl.c:1045)')))

我该怎么办?

python python-requests

47
推荐指数
6
解决办法
10万
查看次数

如何使用requests.post(Python)发送数组?"值错误:解压缩的值太多"

我正在尝试使用requests.post向WheniWork API发送一个请求数组(列表),并且我不断收到两个错误之一.当我将列表作为列表发送时,我得到一个解包错误,当我将其作为字符串发送时,我收到一个错误,要求我提交一个数组.我认为它与请求处理列表的方式有关.以下是示例:

url='https://api.wheniwork.com/2/batch'
headers={"W-Token": "Ilovemyboss"}
data=[{'url': '/rest/shifts', 'params': {'user_id': 0,'other_stuff':'value'}, 'method':'post',{'url': '/rest/shifts', 'params': {'user_id': 1,'other_stuff':'value'}, 'method':'post'}]
r = requests.post(url, headers=headers,data=data)
print r.text

# ValueError: too many values to unpack
Run Code Online (Sandbox Code Playgroud)

只需将数据的值包装在引号中:

url='https://api.wheniwork.com/2/batch'
headers={"W-Token": "Ilovemyboss"}
data="[]" #removed the data here to emphasize that the only change is the quotes
r = requests.post(url, headers=headers,data=data)
print r.text

#{"error":"Please include an array of requests to make.","code":5000}
Run Code Online (Sandbox Code Playgroud)

python api python-requests

46
推荐指数
3
解决办法
4万
查看次数

如何从Python请求中读取响应?

我有两个Python脚本.一个使用Urllib2库,一个使用Requests库.

我发现Requests更容易实现,但我找不到urlib2 read()函数的等价物.例如:

...
response = url.urlopen(req)
print response.geturl()
print response.getcode()
data = response.read()
print data
Run Code Online (Sandbox Code Playgroud)

一旦我建立了我的帖子网址,data = response.read()给我内容 - 我正在尝试连接到vcloud导演api实例,响应显示我可以访问的端点.但是,如果我使用Requests库如下.....

....

def post_call(username, org, password, key, secret):

    endpoint = '<URL ENDPOINT>'
    post_url = endpoint + 'sessions'
    get_url = endpoint + 'org'
    headers = {'Accept':'application/*+xml;version=5.1', \
               'Authorization':'Basic  '+ base64.b64encode(username + "@" + org + ":" + password), \
               'x-id-sec':base64.b64encode(key + ":" + secret)}
    print headers
    post_call = requests.post(post_url, data=None, headers = headers)
    print …
Run Code Online (Sandbox Code Playgroud)

python python-requests

45
推荐指数
4
解决办法
18万
查看次数

python请求包中数据和json参数之间的差异

python Requests包中的data和json参数有什么区别?

从文档中不清楚

这段代码是:

import requests
import json
d = {'a': 1}
response = requests.post(url, data=json.dumps(d))
Run Code Online (Sandbox Code Playgroud)

做任何不同于:

import requests
import json
d = {'a': 1}
response = requests.post(url, json=d)
Run Code Online (Sandbox Code Playgroud)

如果是这样,什么?后者是否自动将content-type标题设置为application/json?

python json python-requests

44
推荐指数
2
解决办法
7370
查看次数

如何使用请求库发送xml正文?

def request():
    #encoded_xml = urllib.urlencode({'XML': read_xml()})
    #encoded_xml = read_xml()
    headers = {'Authorization': AUTH_TOKEN,\
               'developerToken': DEVELOPER_TOKEN,\
               'clientCostumerID': CLIENT_ID}
    content = {'__rdxml': encoded_xml}
    #content = encoded_xml
    #content = {'__rdxml': read_xml2()}
    r = requests.post(URL, data=content,\
        headers=headers)
    return r
Run Code Online (Sandbox Code Playgroud)

这些组合似乎不起作用.

由于某种原因,标头未设置.

python python-requests

43
推荐指数
2
解决办法
8万
查看次数

使用python请求传递csrftoken

你如何通过python模块请求传递csrftoken?这就是我所拥有但它不起作用,而且我不确定将它传入哪个参数(数据,标题,认证...)

import requests
from bs4 import BeautifulSoup

URL = 'https://portal.bitcasa.com/login'

client = requests.session(config={'verbose': sys.stderr})

# Retrieve the CSRF token first
soup = BeautifulSoup(client.get('https://portal.bitcasa.com/login').content)
csrftoken = soup.find('input', dict(name='csrfmiddlewaretoken'))['value']

login_data = dict(username=EMAIL, password=PASSWORD, csrfmiddlewaretoken=csrftoken)
r = client.post(URL, data=login_data, headers={"Referer": "foo"})
Run Code Online (Sandbox Code Playgroud)

每次都有相同的错误消息.

<h1>Forbidden <span>(403)</span></h1>
<p>CSRF verification failed. Request aborted.</p>
Run Code Online (Sandbox Code Playgroud)

python csrf python-requests

43
推荐指数
1
解决办法
6万
查看次数

Python请求.403禁止

我需要解析一个网站,但我得到一个错误403 Forbidden.这是一个代码:

url = 'http://worldagnetwork.com/'
result = requests.get(url)
print(result.content.decode())
Run Code Online (Sandbox Code Playgroud)

它的输出:

<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx</center>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

请说出问题所在.

python python-requests

43
推荐指数
2
解决办法
5万
查看次数

Python的Requests库线程中的Session对象是否安全?

据称Python的流行Requests库在其主页上是线程安全的,但没有给出进一步的细节.如果我调用requests.session(),我可以安全地将此对象传递给多个线程,如下所示:

session = requests.session()
for i in xrange(thread_count):
    threading.Thread(
        target=target,
        args=(session,),
        kwargs={}
    )
Run Code Online (Sandbox Code Playgroud)

并在多个线程中使用相同的连接池发出请求?

如果是这样,这是推荐的方法,还是应该为每个线程提供自己的连接池?(假设所有单个连接池的总大小总计为一个大连接池的大小,如上所述.)每种方法的优缺点是什么?

python connection-pooling thread-safety python-requests

42
推荐指数
2
解决办法
1万
查看次数

使用Python请求测量网站加载时间

我正在尝试构建一个测试我的互联网连接延迟的工具,更具体地说是网站加载时间.我想过使用python 请求模块来加载部分.

问题是,它没有内置功能来衡量获得完整响应所需的时间.为此,我想我会使用该timeit模块.

我不确定的是,如果我像这样运行timeit:

t = timeit.Timer("requests.get('http://www.google.com')", "import requests")
Run Code Online (Sandbox Code Playgroud)

我是否真的在测量响应到达的时间,还是构建,发送,接收等请求所需的时间?我猜我可以忽略那个超时时间,因为我正在测试网络延迟很长时间(~700ms)?

有没有更好的方法以编程方式执行此操作?

python networking delay timeit python-requests

41
推荐指数
2
解决办法
6万
查看次数

我可以更改Python的"请求"模块的连接池大小吗?

(编辑:也许这个错误意味着我错了.这是否表示我的CLIENT的连接池已满?或者SERVER的连接池已满,这是我的客户端给出的错误?)

我试图http使用python threading和requests模块同时发出大量请求.我在日志中看到这个错误:

WARNING:requests.packages.urllib3.connectionpool:HttpConnectionPool is full, discarding connection:
Run Code Online (Sandbox Code Playgroud)

如何增加请求的连接池大小?

python multithreading request python-requests

41
推荐指数
3
解决办法
2万
查看次数