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
34
votes
3 answers

Python - Download File Using Requests, Directly to Memory

The goal is to download a file from the internet, and create from it a file object, or a file like object without ever having it touch the hard drive. This is just for my knowledge, wanting to know if its possible or practical, particularly because…
Anon
  • 2,267
  • 3
  • 34
  • 51
34
votes
4 answers

openssl, python requests error: "certificate verify failed"

If I run the following command from my development box: $ openssl s_client -connect github.com:443 I get the following last line of output: Verify return code: 20 (unable to get local issuer certificate) If I try to do this with requests I get…
chadgh
  • 9,063
  • 8
  • 38
  • 54
34
votes
2 answers

Why does HTTP POST request body need to be JSON enconded in Python?

I ran into this issue when playing around with an external API. I was sending my body data as a dictionary straight into the request and was getting 400 errors: data = { "someParamRange": { "to": 1000, "from": 100 }, …
acpigeon
  • 1,699
  • 9
  • 20
  • 30
33
votes
6 answers

Python requests - quickly know if response is json parsable

I wrote a certain API wrapper using Python's requests library. When it gets a response using requests.get, it attempts to parse as json and takes the raw content if it doesn't work: resp = requests.get(url, ...) try: resp_content =…
tscizzle
  • 11,191
  • 15
  • 54
  • 88
33
votes
2 answers

Why is Python 3 http.client so much faster than python-requests?

I was testing different Python HTTP libraries today and I realized that http.client library seems to perform much much faster than requests. To test it you can run following two code samples. import http.client conn =…
Pawel Miech
  • 7,742
  • 4
  • 36
  • 57
33
votes
3 answers

Python Requests getting ('Connection aborted.', BadStatusLine("''",)) error

def download_torrent(url): fname = os.getcwd() + '/' + url.split('title=')[-1] + '.torrent' try: schema = ('http:') r = requests.get(schema + url, stream=True) with open(fname, 'wb') as f: for chunk in…
eurabilis
  • 331
  • 1
  • 3
  • 7
33
votes
3 answers

Why do I receive a timeout error from Pythons requests module?

I use requests.post(url, headers, timeout=10) and sometimes I received a ReadTimeout exception HTTPSConnectionPool(host='domain.com', port=443): Read timed out. (read timeout=10) Since I already set timeout as 10 seconds, why am I still receiving a…
chrizonline
  • 4,779
  • 17
  • 62
  • 102
33
votes
3 answers

Python Requests and Unicode

I am using the requests library to query the Diffbot API to get contents of an article from a web page url. When I visit a request URL that I create in my browser, it returns a JSON object with the text in Unicode (right?) for example (I shortended…
Javaaaa
  • 3,788
  • 7
  • 43
  • 54
32
votes
3 answers

Set port in requests

I'm attempting to make use of cgminer's API using Python. I'm particularly interested in utilizing the requests library. I understand how to do basic things in requests, but cgminer wants to be a little more specific. I'd like to shrink import…
2mac
  • 1,609
  • 5
  • 20
  • 35
32
votes
4 answers

Python 'requests' library - define specific DNS?

In my project I'm handling all HTTP requests with python requests library. Now, I need to query the http server using specific DNS - there are two environments, each using its own DNS, and changes are made independently. So, when the code is…
Taku
  • 562
  • 1
  • 6
  • 15
32
votes
1 answer

Create url without request execution

I am currently using the python requests package to make JSON requests. Unfortunately, the service which I need to query has a daily maximum request limit. Right know, I cache the executed request urls, so in case I come go beyond this limit, I…
Andy
  • 9,483
  • 12
  • 38
  • 39
31
votes
2 answers

How to extract the HTTP error text from a requests response?

I have the following code: tries = 10 for n in range(tries): try: .... responsedata = requests.get(url, data=data, headers=self.hed, verify=False) responsedata.raise_for_status() .. if .... : …
Programmer120
  • 2,362
  • 9
  • 30
  • 48
31
votes
1 answer

Requests — always call raise_for_status

I'd like to remove the repeated x.raise_for_status() lines: x = requests.get(url1) x.raise_for_status() y = requests.delete(url2) y.raise_for_status() z = requests.post(url3, data={'foo': 'bar'}) z.raise_for_status() How can I call…
Max Malysh
  • 29,384
  • 19
  • 111
  • 115
31
votes
2 answers

sending raw data in python requests

I'm trying to send a POST request with python requests, containing the following…
idik
  • 872
  • 2
  • 10
  • 19
31
votes
4 answers

Convert text data from requests object to dataframe with pandas

Using requests I am creating an object which is in .csv format. How can I then write that object to a DataFrame with pandas? To get the requests object in text format: import requests import pandas as pd url = r'http://test.url' r =…
sparrow
  • 10,794
  • 12
  • 54
  • 74