req =urllib.request.Request('ftp://example.com/ ') Note that urllib.request makes use of the same Request interface to handle all URL schemes. For example, you can make an FTP request like so: In the case of HTTP, there are two extra things that Request objects allow you to do: First, you can pass data to be sent to the server. Second, you can pass extra information (“metadata”) about the data or about the request itself, to the server - this information is sent as HTTP “headers”. Let’s look at each of these in turn. Sometimes you want to send data to a URL (often the URL will refer to a CGI (Common Gateway Interface) script or other web application). With HTTP, this is often done using what’s known as a POST request. This is often what your browser does when you submit a HTML form that you filled in on the web. Not all POSTs have to come from forms: you can use a POST to transmit arbitrary data to your own application. In the common case of HTML forms, the data needs to be encoded in a standard way, and then passed to the Request object as the$_{data}$argument. The encoding is done using a function from the$_{urllib.parse}$ library. Note that other encodings are sometimes required (e.g. for file upload from HTML forms - see HTML Specification, Form Submission for more details). If you do not pass the $_{data}$argument, urllib uses a GET request. One way in which GET and POST requests differ is that POST requests often have “side-effects”: they change the state of the system in some way (for example by placing an order with the website for a hundredweight of tinned spam to be delivered to your door). Though the HTTP standard makes it clear that POSTs are intended to always cause side-effects, and GET requests never to cause side-effects, nothing prevents a GET request from having side-effects, nor a POST requests from having no side-effects. Data can also be passed in an HTTP GET request by encoding it in the URL itself. This is done as follows: (continues on next page) 3 >>> import urllib.request >>> import urllib.parse >>> data ={} >>> data['name'] ='Somebody Here ' >>> data['location'] ='Northampton' >>> data['language '] ='Python' >>> url_values =urllib.parse.urlencode(data) >>> print(url_values) # The order may differ from below. name=Somebody+Here&language=Python&location=Northampton >>> url ='http://www.example.com/example.cgi ' import urllib.parse import urllib.request url ='http://www.someserver.com/cgi-bin/register.cgi ' values={ 'name':'Michael Foord ', 'location':'Northampton', 'language ':'Python'} data =urllib.parse.urlencode(values) data =data.encode('ascii ') # data should be bytes req =urllib.request.Request(url, data) with urllib.request.urlopen(req) as response: the_page=response.read() 2.1 Data import urllib.request req =urllib.request.Request('http://www.voidspace.org.uk ') with urllib.request.urlopen(req) as response: the_page=response.read()