forked from purcellconsult/Cracking-Python-Bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10_http_requests_option.py
107 lines (84 loc) · 3.05 KB
/
10_http_requests_option.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
########################################
# HTTP requests fundamentals in python
# -------------------------------------
#
# Learn the various ways to make HTTP/1.1 requests
# in python through code examples.
#
# requests: https://pypi.org/project/requests
# urllib3: https://urllib3.readthedocs.io/en/latest
# urllib2: https://docs.python.org/2/library/urllib2.html
# httplib2: https://github.com/httplib2/httplib2
#
# By Doug Purcell
# http://www.purcellconsult.com
#
##########################################
import requests
import urllib
from urllib.request import urlopen
import urllib3
import httplib2
# Requests Overview
# -----------------
# Fully supports a restful API
# get, post, put, delete, methods
# international domains and urls
# keep-alive & connection pooling (uses urllib3 for this)
# browser-style SSL verification
# making a simple HTTP/1.1 request
wiki = requests.get('https://en.wikipedia.org/wiki/Main_Page')
print(wiki.status_code) # checks the status codes: 200
print(requests.codes['temporary_redirect']) # 307
# after a server returns the response you want, can collect content
t = wiki.text # response in bytes
c = wiki.content # response in Unicode
print(dir(wiki)) # see all of the cool functionality you can do!
print(wiki.headers['Content-Type'])
# The urllib module
# ------------------
# URL Handing Modules: https://docs.python.org/3/library/urllib.html
# contains several modules for working with URLS such as
# urllib.request, urllib.error, urllib.parse, and urllib.robotparser
# returns the URL of the resource received
url = urllib.request.urlopen('https://www.gocomics.com/comics/popular')
# returns URL of the resource retrieved
link = url.geturl()
# returns meta info of the page like headers
code = url.info()
# returns the HTTP status of the response
status = url.getcode()
print(url)
print(link)
print(code)
print(status)
# The urllib2 module
# ------------------
# A module that's been split across various modules
# in python3 named urllib.request and urllib.error.
# since the urllib2 module has been split across several modules,
# must do the following: from urllib.request import urlopen
site = urlopen('https://www.latimes.com')
content = ''
for text in site:
content += str(text)
# The urllib3 module
# ------------------
# A powerful HTTP client for python. Some of its features:
# Thread safety
# Connection pooling
# Client-side SSL/TLS verification
# File uploads with multipart encoding
http = urllib3.PoolManager() # needed to make requests
r = http.request('GET', 'http://www.yahoo.com')
print(r.data.decode('utf-8'))
h = httplib2.Http('.cache')
(resp_headers, content) = h.request('http://www.example.org', 'GET')
h = httplib2.Http(".cache")
h.add_credentials('name', 'password')
(resp, content) = h.request("https://example.org/chapter/2",
"PUT", body="This is text",
headers={'content-type':'text/plain'} )
# Why so many options for making HTTP requests?
# There are many talented developers who feel they can
# create a better solution then the other ones.