Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python http request with token

how and with which python library is it possible to make an httprequest (https) with a user:password or a token?

basically the equivalent to curl -u user:pwd https://www.mysite.com/

thank you

like image 256
aschmid00 Avatar asked Jul 28 '10 17:07

aschmid00


People also ask

How do you access token in Python?

Obtain Access TokenUse your client ID and client secret to obtain an auth token. You will add the auth token to the header of each API request. The following Python example shows how to obtain an auth token and create the Authorization header using the token.

How do you send a GET request with Bearer Token in Python?

To send a GET request with a Bearer Token authorization header using Python, you need to make an HTTP GET request and provide your Bearer Token with the Authorization: Bearer {token} HTTP header.

How do I authenticate API requests in Python?

There are a few common authentication methods for REST APIs that can be handled with Python Requests. The simplest way is to pass your username and password to the appropriate endpoint as HTTP Basic Auth; this is equivalent to typing your username and password into a website.


2 Answers

use python requests : Http for Humans

import requests

requests.get("https://www.mysite.com/", auth=('username','pwd'))

you can also use digest auth...

like image 118
jassinm Avatar answered Sep 29 '22 22:09

jassinm


If you need to make thread-safe requests, use pycurl (the python interface to curl):

import pycurl
from StringIO import StringIO

response_buffer = StringIO()
curl = pycurl.Curl()

curl.setopt(curl.URL, "https://www.yoursite.com/")

# Setup the base HTTP Authentication.
curl.setopt(curl.USERPWD, '%s:%s' % ('youruser', 'yourpassword'))

curl.setopt(curl.WRITEFUNCTION, response_buffer.write)

curl.perform()
curl.close()

response_value = response_buffer.getvalue()

Otherwise, use urllib2 (see other responses for more info) as it's builtin and the interface is much cleaner.

like image 27
Sam Dolan Avatar answered Sep 29 '22 22:09

Sam Dolan