Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from http.client import HTTPSConnection
- from base64 import b64encode
- #This sets up the https connection
- c = HTTPSConnection("www.google.com")
- #we need to base 64 encode it
- #and then decode it to acsii as python 3 stores it as a byte string
- userAndPass = b64encode(b"username:password").decode("ascii")
- headers = { 'Authorization' : 'Basic %s' % userAndPass }
- #then connect
- c.request('GET', '/', headers=headers)
- #get the response back
- res = c.getresponse()
- # at this point you could check the status etc
- # this gets the page text
- data = res.read()
- import requests
- r = requests.get('https://my.website.com/rest/path', auth=('myusername', 'mybasicpass'))
- print(r.text)
- ubuntu@hostname:/home/ubuntu$ python3
- Python 3.4.3 (default, Oct 14 2015, 20:28:29)
- [GCC 4.8.4] on linux
- Type "help", "copyright", "credits" or "license" for more information.
- >>> import requests
- >>> r = requests.get('https://my.website.com/rest/path', auth=('myusername', 'mybasicpass'))
- >>> dir(r)
- ['__attrs__', '__bool__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_content', '_content_consumed', 'apparent_encoding', 'close', 'connection', 'content', 'cookies', 'elapsed', 'encoding', 'headers', 'history', 'iter_content', 'iter_lines', 'json', 'links', 'ok', 'raise_for_status', 'raw', 'reason', 'request', 'status_code', 'text', 'url']
- >>> r.content
- b'{"battery_status":0,"margin_status":0,"timestamp_status":null,"req_status":0}'
- >>> r.text
- '{"battery_status":0,"margin_status":0,"timestamp_status":null,"req_status":0}'
- >>> r.status_code
- 200
- >>> r.headers
- CaseInsensitiveDict({'x-powered-by': 'Express', 'content-length': '77', 'date': 'Fri, 20 May 2016 02:06:18 GMT', 'server': 'nginx/1.6.3', 'connection': 'keep-alive', 'content-type': 'application/json; charset=utf-8'})
- import httplib2
- h = httplib2.Http(".cache")
- h.add_credentials('name', 'password') # Basic authentication
- resp, content = h.request("https://host/path/to/resource", "POST", body="foobar")
- import pycurl
- import cStringIO
- import base64
- headers = { 'Authorization' : 'Basic %s' % base64.b64encode("username:password") }
- response = cStringIO.StringIO()
- conn = pycurl.Curl()
- conn.setopt(pycurl.VERBOSE, 1)
- conn.setopt(pycurlHTTPHEADER, ["%s: %s" % t for t in headers.items()])
- conn.setopt(pycurl.URL, "https://host/path/to/resource")
- conn.setopt(pycurl.POST, 1)
- conn.setopt(pycurl.SSL_VERIFYPEER, False)
- conn.setopt(pycurl.SSL_VERIFYHOST, False)
- conn.setopt(pycurl.WRITEFUNCTION, response.write)
- post_body = "foobar"
- conn.setopt(pycurl.POSTFIELDS, post_body)
- conn.perform()
- http_code = conn.getinfo(pycurl.HTTP_CODE)
- if http_code is 200:
- print response.getvalue()
- #!/usr/bin/env python3
- import urllib.request
- import ssl
- import certifi
- context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
- context.verify_mode = ssl.CERT_REQUIRED
- context.load_verify_locations(certifi.where())
- httpsHandler = urllib.request.HTTPSHandler(context = context)
- manager = urllib.request.HTTPPasswordMgrWithDefaultRealm()
- manager.add_password(None, 'https://domain.com/', 'username', 'password')
- authHandler = urllib.request.HTTPBasicAuthHandler(manager)
- opener = urllib.request.build_opener(httpsHandler, authHandler)
- # Used globally for all urllib.request requests.
- # If it doesn't fit your design, use opener directly.
- urllib.request.install_opener(opener)
- response = urllib.request.urlopen('https://domain.com/some/path')
- print(response.read())
Add Comment
Please, Sign In to add comment