Changing the referer URL in python requests

According to http://docs.python-requests.org/en/latest/user/advanced/#session-objects , you should be able to do: s = requests.Session() s.headers.update({‘referer’: my_referer}) s.get(url) Or just: requests.get(url, headers={‘referer’: my_referer}) Your headers dict will be merged with the default/session headers. From the docs: Any dictionaries that you pass to a request method will be merged with the session-level values that are set. The method-level … Read more

How to specify python requests http put body?

Quoting from the docs data – (optional) Dictionary or bytes to send in the body of the Request. So this should work (not tested): filepath=”yourfilename.txt” with open(filepath) as fh: mydata = fh.read() response = requests.put(‘https://api.elasticemail.com/attachments/upload’, data=mydata, auth=(‘omer’, ‘b01ad0ce’), headers={‘content-type’:’text/plain’}, params={‘file’: filepath} )

Python : Trying to POST form using requests

You can use the Session object import requests headers = {‘User-Agent’: ‘Mozilla/5.0’} payload = {‘username’:’niceusername’,’password’:’123456′} session = requests.Session() session.post(‘https://admin.example.com/login.php’,headers=headers,data=payload) # the session instance holds the cookie. So use it to get/post later. # e.g. session.get(‘https://example.com/profile’)

Passing csrftoken with python Requests

If you are going to set the referrer header, then for that specific site you need to set the referrer to the same URL as the login page: import sys import requests URL = ‘https://portal.bitcasa.com/login’ client = requests.session() # Retrieve the CSRF token first client.get(URL) # sets cookie if ‘csrftoken’ in client.cookies: # Django 1.6 … Read more

using requests with TLS doesn’t give SNI support

The current version of Requests should be just fine with SNI. Further down the GitHub issue you can see the requirements: pyOpenSSL ndg-httpsclient pyasn1 Try installing those packages and then give it another shot. EDIT: As of Requests v2.12.1, ndg-httpsclient and pyasn1 are no longer required. The full list of required packages is now: pyOpenSSL … Read more

Python Requests requests.exceptions.SSLError: [Errno 8] _ssl.c:504: EOF occurred in violation of protocol

Reposting this here for others from the requests issue page: Requests’ does not support doing this before version 1. Subsequent to version 1, you are expected to subclass the HTTPAdapter, like so: from requests.adapters import HTTPAdapter from requests.packages.urllib3.poolmanager import PoolManager import ssl class MyAdapter(HTTPAdapter): def init_poolmanager(self, connections, maxsize, block=False): self.poolmanager = PoolManager(num_pools=connections, maxsize=maxsize, block=block, ssl_version=ssl.PROTOCOL_TLSv1) … Read more

How can I send an xml body using requests library?

Just send xml bytes directly: #!/usr/bin/env python2 # -*- coding: utf-8 -*- import requests xml = “””<?xml version=’1.0′ encoding=’utf-8′?> <a>б</a>””” headers = {‘Content-Type’: ‘application/xml’} # set what your server accepts print requests.post(‘http://httpbin.org/post’, data=xml, headers=headers).text Output { “origin”: “x.x.x.x”, “files”: {}, “form”: {}, “url”: “http://httpbin.org/post”, “args”: {}, “headers”: { “Content-Length”: “48”, “Accept-Encoding”: “identity, deflate, compress, gzip”, … Read more

Python requests exception handling

Assuming you did import requests, you want requests.ConnectionError. ConnectionError is an exception defined by requests. See the API documentation here. Thus the code should be: try: requests.get(‘http://www.google.com’) except requests.ConnectionError: # handle the exception The original link to the Python v2 API documentation from the original answer no longer works.

Get file size using python-requests, while only getting the header

Send a HEAD request: >>> import requests >>> 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, 11 Jan 2013 02:32:34 GMT’, ‘last-modified’: ‘Fri, 04 Jan 2013 01:17:22 GMT’, ‘server’: ‘Apache/2.2.3 (CentOS)’, ‘vary’: ‘Accept-Encoding’} A HEAD request is like a GET request that only downloads the headers. Note … Read more