Learning Python Network Programming

(Sean Pound) #1

HTTP and Working with the Web


The Session object has the same interface as the requests module, so we use its
get() method in the same way as we use the requests.get()method. Now, any
cookies encountered are stored in the Session object, and they will be sent with
corresponding requests when we use the get() method in the future.


Redirects are also automatically followed, in the same way as when using urllib,
and any redirected requests are captured in the history attribute.


The different HTTP methods are easily accessible, they have their own functions:





response = requests.head('http://www.google.com')








response.status_code





200





response.text





''


Custom headers are added to to requests in a similar way as they are when
using urllib:





headers = {'User-Agent': 'Mozilla/5.0 Firefox 24'}








response = requests.get('http://www.debian.org', headers=headers)





Making requests with query strings is a straightforward process:





params = {':action': 'search', 'term': 'Are you quite sure this
is a cheese shop?'}








response = requests.get('http://pypi.python.org/pypi',
params=params)








response.url





'https://pypi.python.org/pypi?%3Aaction=search&term=Are+you+quite+sur
e+this+is+a+cheese+shop%3F'


The Requests library takes care of all the encoding and formatting for us.


Posting is similarly simplified, although we use the data keyword argument here:





data = {'P', 'Python'}








response = requests.post('http://search.debian.org/cgi-
bin/omega', data=data)




Free download pdf