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
53
votes
6 answers

Fetch a file from a local url with Python requests?

I am using Python's requests library in one method of my application. The body of the method looks like this: def handle_remote_file(url, **kwargs): response = requests.get(url, ...) buff = StringIO.StringIO() …
Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
51
votes
6 answers

How to install requests module in Python 3.4, instead of 2.7

I have both Python 2.7 and 3.4 installed on my Ubuntu 14.04 machine. I want to install the 'requests' module so it is accessible from Py3.4. When I issued pip install requests on my terminal cmd line I got back: "Requirement already satisfied (use…
Thom Rogers
  • 1,385
  • 2
  • 20
  • 33
49
votes
15 answers

Why requests raise this exception "check_hostname requires server_hostname"?

p={ 'http':'http://my correct proxy here', 'https':'https://my correct proxy here' } self.response=requests.get(url=url,headers=self.headers,timeout=(6,15),proxies=p) And then it raise the exception: Traceback (most recent call last): …
KEVIN
  • 821
  • 1
  • 4
  • 9
49
votes
8 answers

Upload Image using POST form data in Python-requests

I'm working with wechat APIs ... here I've to upload an image to wechat's server using this API http://admin.wechat.com/wiki/index.php?title=Transferring_Multimedia_Files url =…
micheal
  • 1,283
  • 3
  • 14
  • 23
49
votes
2 answers

python requests module and connection reuse

I am working with python's requests module for HTTP communication, and I am wondering how to reuse already-established TCP connections? The requests module is stateless and if I repeatedly call get for the same URL, wouldn't it create a new…
gmemon
  • 2,573
  • 5
  • 32
  • 37
49
votes
5 answers

Get the description of a status code in Python Requests

I would like to be able to enter a server response code and have Requests tell me what the code means. For example, code 200 --> ok I found a link to the source code which shows the dictionary structure of the codes and descriptions. I see that…
Roberto
  • 2,054
  • 4
  • 31
  • 46
49
votes
1 answer

Using Python Requests: Sessions, Cookies, and POST

I am trying to scrape some selling data using the StubHub API. An example of this data seen here: https://sell.stubhub.com/sellapi/event/4236070/section/null/seatmapdata You'll notice that if you try and visit that url without logging into…
user2238685
  • 707
  • 1
  • 6
  • 8
48
votes
1 answer

Python requests is slow and takes very long to complete HTTP or HTTPS request

When requesting a web resource or website or web service with the requests library, the request takes a long time to complete. The code looks similar to the following: import requests requests.get("https://www.example.com/") This request takes over…
vauhochzett
  • 2,732
  • 2
  • 17
  • 40
48
votes
7 answers

Python requests: URL base in Session

When using a Session, it seems you need to provide the full URL each time, e.g. session = requests.Session() session.get('http://myserver/getstuff') session.get('http://myserver/getstuff2') This gets a little tedious. Is there a way to do something…
paj28
  • 2,210
  • 3
  • 25
  • 37
48
votes
14 answers

Python - requests.exceptions.SSLError - dh key too small

I'm scraping some internal pages using Python and requests. I've turned off SSL verifications and warnings. requests.packages.urllib3.disable_warnings() page = requests.get(url, verify=False) On certain servers I receive an SSL error I can't get…
Feocco
  • 640
  • 1
  • 6
  • 12
48
votes
2 answers

Difference between using requests.get() and requests.session().get()?

Sometimes I see people invoke web API using requests.Session object: client = requests.session() resp = client.get(url='...') But sometimes they don't: resp = requests.get(url='...') Can somebody explain when should we use Session and when we…
Fred Pym
  • 2,149
  • 1
  • 20
  • 29
48
votes
3 answers

Try/except when using Python requests module

Doing some API testing and trying to create a function that given an inputted URL it will return the json response, however if a HTTP error is the response an error message will be returned. I was using urllib2 before, but now trying to use…
mroriel
  • 605
  • 1
  • 5
  • 7
47
votes
2 answers

HTTP requests.post timeout

In my code below, I use requests.post. What are the possibilities to simply continue if the site is down? I have the following code: def post_test(): import requests url = 'http://example.com:8000/submit' payload = {'data1': 1,…
Zorgmorduk
  • 1,265
  • 2
  • 16
  • 32
47
votes
7 answers

Python Requests: Don't wait for request to finish

In Bash, it is possible to execute a command in the background by appending &. How can I do it in Python? while True: data = raw_input('Enter something: ') requests.post(url, data=data) # Don't wait for it to finish. print('Sending POST…
octosquidopus
  • 3,517
  • 8
  • 35
  • 53
47
votes
2 answers

Size of raw response in bytes

I need to make a HTTP request and determine the response size in bytes. I have always used request for simple HTTP requests, but I am wondering if I can achieve this using raw? >>> r = requests.get('https://github.com/', stream=True) >>> r.raw My…
ewhitt
  • 897
  • 1
  • 12
  • 18