Guest User

Untitled

a guest
Sep 26th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. from http.client import HTTPSConnection
  2. from base64 import b64encode
  3. #This sets up the https connection
  4. c = HTTPSConnection("www.google.com")
  5. #we need to base 64 encode it
  6. #and then decode it to acsii as python 3 stores it as a byte string
  7. userAndPass = b64encode(b"username:password").decode("ascii")
  8. headers = { 'Authorization' : 'Basic %s' % userAndPass }
  9. #then connect
  10. c.request('GET', '/', headers=headers)
  11. #get the response back
  12. res = c.getresponse()
  13. # at this point you could check the status etc
  14. # this gets the page text
  15. data = res.read()
  16.  
  17. import requests
  18.  
  19. r = requests.get('https://my.website.com/rest/path', auth=('myusername', 'mybasicpass'))
  20. print(r.text)
  21.  
  22. ubuntu@hostname:/home/ubuntu$ python3
  23. Python 3.4.3 (default, Oct 14 2015, 20:28:29)
  24. [GCC 4.8.4] on linux
  25. Type "help", "copyright", "credits" or "license" for more information.
  26. >>> import requests
  27. >>> r = requests.get('https://my.website.com/rest/path', auth=('myusername', 'mybasicpass'))
  28. >>> dir(r)
  29. ['__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']
  30. >>> r.content
  31. b'{"battery_status":0,"margin_status":0,"timestamp_status":null,"req_status":0}'
  32. >>> r.text
  33. '{"battery_status":0,"margin_status":0,"timestamp_status":null,"req_status":0}'
  34. >>> r.status_code
  35. 200
  36. >>> r.headers
  37. 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'})
  38.  
  39. import httplib2
  40.  
  41. h = httplib2.Http(".cache")
  42.  
  43. h.add_credentials('name', 'password') # Basic authentication
  44.  
  45. resp, content = h.request("https://host/path/to/resource", "POST", body="foobar")
  46.  
  47. import pycurl
  48. import cStringIO
  49. import base64
  50.  
  51. headers = { 'Authorization' : 'Basic %s' % base64.b64encode("username:password") }
  52.  
  53. response = cStringIO.StringIO()
  54. conn = pycurl.Curl()
  55.  
  56. conn.setopt(pycurl.VERBOSE, 1)
  57. conn.setopt(pycurlHTTPHEADER, ["%s: %s" % t for t in headers.items()])
  58.  
  59. conn.setopt(pycurl.URL, "https://host/path/to/resource")
  60. conn.setopt(pycurl.POST, 1)
  61.  
  62. conn.setopt(pycurl.SSL_VERIFYPEER, False)
  63. conn.setopt(pycurl.SSL_VERIFYHOST, False)
  64.  
  65. conn.setopt(pycurl.WRITEFUNCTION, response.write)
  66.  
  67. post_body = "foobar"
  68. conn.setopt(pycurl.POSTFIELDS, post_body)
  69.  
  70. conn.perform()
  71.  
  72. http_code = conn.getinfo(pycurl.HTTP_CODE)
  73. if http_code is 200:
  74. print response.getvalue()
  75.  
  76. #!/usr/bin/env python3
  77.  
  78.  
  79. import urllib.request
  80. import ssl
  81.  
  82. import certifi
  83.  
  84.  
  85. context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
  86. context.verify_mode = ssl.CERT_REQUIRED
  87. context.load_verify_locations(certifi.where())
  88. httpsHandler = urllib.request.HTTPSHandler(context = context)
  89.  
  90. manager = urllib.request.HTTPPasswordMgrWithDefaultRealm()
  91. manager.add_password(None, 'https://domain.com/', 'username', 'password')
  92. authHandler = urllib.request.HTTPBasicAuthHandler(manager)
  93.  
  94. opener = urllib.request.build_opener(httpsHandler, authHandler)
  95.  
  96. # Used globally for all urllib.request requests.
  97. # If it doesn't fit your design, use opener directly.
  98. urllib.request.install_opener(opener)
  99.  
  100. response = urllib.request.urlopen('https://domain.com/some/path')
  101. print(response.read())
Add Comment
Please, Sign In to add comment