Chapter 11. Network and Web Programming
This chapter is about various topics related to using Python in networked and distributed applications. Topics are split between using Python as a client to access existing services and using Python to implement networked services as a server. Common techniques for writing code involving cooperating or communicating with interpreters are also given.
11.1. Interacting with HTTP Services As a Client
Problem
You need to access various services via HTTP as a client. For example, downloading data or interacting with a REST-based API.
Solution
For simple things, it’s usually easy enough to use the
urllib.request module. For example, to send a simple HTTP GET
request to a remote service, do something like this:
fromurllibimportrequest,parse# Base URL being accessedurl='http://httpbin.org/get'# Dictionary of query parameters (if any)parms={'name1':'value1','name2':'value2'}# Encode the query stringquerystring=parse.urlencode(parms)# Make a GET request and read the responseu=request.urlopen(url+'?'+querystring)resp=u.read()
If you need to send the query parameters in the request body using a
POST method, encode them and supply them as an optional argument to
urlopen() like this:
fromurllibimportrequest,parse# Base URL being accessedurl='http://httpbin.org/post'# Dictionary of query parameters (if any)parms={'name1':'value1','name2':'value2'}# Encode the query stringquerystring=parse.urlencode(parms)
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access