Questions tagged [python-requests]

USE ONLY FOR THE PYTHON REQUESTS LIBRARY. Requests is a full-featured Python HTTP library with an easy-to-use, logical API.

Official web site

Requests is an HTTP library written in Python under the Apache2 license.

It's meant to simplify HTTP 1.1 related tasks with several functionality out of the box. For example, there’s no need to manually add query strings to your URLs or to form-encode your POST data. Keep-alive and HTTP connection pooling are 100% automatic.

Features:

  • International Domains and URLs
  • Keep-Alive & Connection Pooling
  • Sessions with Cookie Persistence
  • Browser-style SSL Verification
  • Automatic Content Decoding
  • Basic/Digest Authentication
  • Elegant Key/Value Cookies
  • Automatic Decompression
  • Unicode Response Bodies
  • Multipart File Uploads
  • Streaming Downloads
  • Connection Timeouts
  • .netrc support
  • Chunked Requests
  • Python 2.6—3.8
  • Thread-safe.
21751 questions
135
votes
6 answers

Download and save PDF file with Python requests module

I am trying to download a PDF file from a website and save it to disk. My attempts either fail with encoding errors or result in blank PDFs. In [1]: import requests In [2]: url = 'http://www.hrecos.org//images/Data/forweb/HRTVBSH.Metadata.pdf' In…
Jim
  • 1,740
  • 2
  • 13
  • 18
129
votes
6 answers

How do I use basic HTTP authentication with the Python Requests library?

I'm trying to use basic HTTP authentication in Python. I am using the Requests library: auth = requests.post('http://' + hostname, auth=HTTPBasicAuth(user, password)) request = requests.get('http://' + hostname + '/rest/applications') Response form…
oleksii
  • 1,367
  • 2
  • 10
  • 11
123
votes
4 answers

How can I use cookies in Python Requests?

I am trying to log in to a page and access another link in the page. I get a "405 Not Allowed" error from this attempt: payload={'username'=,'password'=} with session() as s: r = c.post(, data=payload) print(r) …
user1474157
  • 1,379
  • 2
  • 11
  • 13
118
votes
9 answers

How to make python Requests work via SOCKS proxy

I'm using the great Requests library in my Python script: import requests r = requests.get("http://example.com") print(r.text) I would like to use a SOCKS proxy, how can I do that? Requests seems to only support HTTP proxies.
lithuak
  • 6,028
  • 9
  • 42
  • 54
117
votes
5 answers

Python Requests - How to use system ca-certificates (debian/ubuntu)?

I've installed a self-signed root ca cert into debian's /usr/share/ca-certificates/local and installed them with sudo dpkg-reconfigure ca-certificates. At this point true | gnutls-cli mysite.local is happy, and true | openssl s_client -connect…
ThorSummoner
  • 16,657
  • 15
  • 135
  • 147
114
votes
2 answers

How to measure server response time for Python requests POST-request

I create POST requests with requests as follows, with a specified timeout threshold: response = requests.post(url, data=post_fields, timeout=timeout) However, to determine a "good" threshold, I need to benchmark the server response times. How do I…
Shuzheng
  • 11,288
  • 20
  • 88
  • 186
113
votes
6 answers

How to implement retry mechanism into Python Requests library?

I would like to add a retry mechanism to Python Requests library, so scripts that are using it will retry for non-fatal errors. At this moment I do consider three kind of errors to be recoverable: HTTP return codes 502, 503, 504 host not found…
sorin
  • 161,544
  • 178
  • 535
  • 806
107
votes
20 answers

Python (pip) - RequestsDependencyWarning: urllib3 (1.9.1) or chardet (2.3.0) doesn't match a supported version

I found several pages about this issue but none of them solved my problem. Even if I do a : pip show I get : /usr/local/lib/python2.7/dist-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.9.1) or chardet (2.3.0) doesn't match…
NuX_o
  • 1,173
  • 2
  • 7
  • 9
103
votes
2 answers

Sending SOAP request using Python Requests

Is it possible to use Python's requests library to send a SOAP request?
Deepankar Bajpeyi
  • 5,661
  • 11
  • 44
  • 64
100
votes
3 answers

python requests get cookies

x = requests.post(url, data=data) print x.cookies I used the requests library to get some cookies from a website, but I can only get the cookies from the Response, how to get the cookies from the Request? Thanks!
Danfi
  • 1,182
  • 3
  • 10
  • 14
98
votes
2 answers

Access Lovoo API using Python

I am hoping to make use of the lovoo API, but don't really know how to start. After running Charles proxy and looking at the traffic, I have come to the following conclusion: First a GET to https://api.lovoo.com/oauth/requestToken? is sent as soon…
ChaChaPoly
  • 1,811
  • 5
  • 17
  • 39
94
votes
12 answers

Unable to get local issuer certificate when using requests

here is my code 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…
yuan
  • 1,701
  • 2
  • 13
  • 24
92
votes
7 answers

ImportError: No module named 'Queue'

I am trying to import requests module, but I got this error my python version is 3.4 running on ubuntu 14.04 >>> import requests Traceback (most recent call last): File…
Ali Faki
  • 3,928
  • 3
  • 17
  • 25
92
votes
1 answer

Python requests speed up using keep-alive

In the HTTP protocol you can send many requests in one socket using keep-alive and then receive the response from server at once, so that will significantly speed up whole process. Is there any way to do this in python requests lib? Or are there any…
PaulOverflow
  • 1,091
  • 1
  • 10
  • 12
90
votes
9 answers

Python: download a file from an FTP server

I'm trying to download some public data files. I screenscrape to get the links to the files, which all look something like this: ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/nhanes/2001-2002/L28POC_B.xpt I can't find any documentation on the…
user1507455
  • 1,083
  • 2
  • 10
  • 10