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:
from
urllib
import
request
,
parse
# Base URL being accessed
url
=
'http://httpbin.org/get'
# Dictionary of query parameters (if any)
parms
=
{
'name1'
:
'value1'
,
'name2'
:
'value2'
}
# Encode the query string
querystring
=
parse
.
urlencode
(
parms
)
# Make a GET request and read the response
u
=
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:
from
urllib
import
request
,
parse
# Base URL being accessed
url
=
'http://httpbin.org/post'
# Dictionary of query parameters (if any)
parms
=
{
'name1'
:
'value1'
,
'name2'
:
'value2'
}
# Encode the query string
querystring
=
parse
.
urlencode
(
parms ...
Get Python Cookbook, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.