Internet Protocol Libraries - Python
PyCon India 2010, Bangalore
Let us look at the some of Python Standard Library modules which
formulate and make available various Internet Protocol and help us
build good Internet Applications.
There are 3 versions of this presentation:
©2010, licensed under a Creative Commons
Attribution/Share-Alike (BY-SA) license.
- Python Core Developer.
- Maintainer of urllib2 and related modules.
- Software Engineer at Akamai, India.
- Python 3.2a2+ (This is the trunk)
- Python 3.1.2 (release31-maint)
- Python 2.7 (release27-maint)
Unsupported versions:
- Python 3.0 is not there!
- Python 2.6.5 (release26-maint)
http://www.python.org/dev/
- Most of the modules are built on top of socket module.
- They are highlevel modules implmenting Specifications mostly from RFC.
- Provide commonly used helper functions.
- Mercurial
- Google Code (and google-cl)
- youtube-dl
- Bittorrent
- Mailman
- webbrowser
- cgi
- wsgiref
- uuid
- less commonly used protocol modules.
- ftplib
- poplib
- imaplib
- smtplib
- telnetlib
- httplib -> http.client
- cookiekib -> http.cookiejar
- Cookie -> http.cookies
- BaseHTTPServer -> http.server
- SimpleHTTPServer -> http.server
- CGIHTTPServer -> http.server
- SocketServer -> socketserver
- xmlrpclib -> xmlrpc.client
- SimpleXMLRPCServer -> xmlrpc.server
- DocXMLRPCServer -> xmlrpc.server
- Module for Handling all different kinds of browsers
url = 'http://www.python.org/'
webbrowser.open_new_tab(url + 'doc/')
webbrowser.open_new(url)
- Support for Common Gateway Interface Scripts
print "Content-Type: text/html"
print
print "<h1>Hello,World</h1>"
- FieldStorage class for handling forms.
import cgitb
cgitb.enable()
- The Web Server Gateway Interface (WSGI)
- PEP 333
- Django, TurboGears, App Engine, all many different Web Frameworks.
- Standardization helps. You speak one language and follow similar protocols.
- wsgiref is the reference implementation of WSGI Specification.
from wsgiref.simple_server import make_server
def hello_world_app(environ, start_response):
status = '200 OK'
headers = [('Content-type', 'text/plain')]
start_response(status, headers)
return ["Hello World"]
httpd = make_server('', 8000, hello_world_app)
httpd.serve_forever()
- Universally Unique Identifier (RFC 4122)
>>> import uuid
>>> print uuid.uuid1()
32fb97c6-c8d3-11df-a47f-00216bad94a4
>>> print uuid.uuid4()
f9e0a0cd-23dd-46c8-95d6-e0bb0198c121
- Higher Level Interface for Fetching Data from the Web.
- Clients/ Client libraries which handle URLs.
- urllib.urlopen
- Next Version of urllib.
- Create a Request Object.
- Have different Handlers depending upon the Protocol.
- Get the Response by the corresponding Handler object.
- Parse the URLs into its constituents.
- It is required when are building bigger applications.
- Referencing sections within pages.
- Parse IPv6 URLs too.
- It is the underlying layer to the urllib.
- It implements HTTP/1.1 spec.
- Implements the HTTP States on top of socket.
- Defines the FTP class and related items. The FTP class implments the client side FTP protocol.
- Internally used by urllib module for handling ftp urls.
- Got support FTP TLS from Python 2.7 onwards.
- Handling synchronous network requests.
- Helper classes for Mixins.
+------------+
| BaseServer |
+------------+
|
v
+-----------+ +------------------+
| TCPServer |------->| UnixStreamServer |
+-----------+ +------------------+
|
v
+-----------+ +--------------------+
| UDPServer |------->| UnixDatagramServer |
+-----------+ +--------------------+
- BaseHTTPServer
- SimpleHTTPServer
- CGIHTTPServer
python -m SimpleHTTPServer 8000
- XML-RPC client code; it handles all the details of translating between conformable Python objects and XML on the wire.
import xmlrpclib
proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
print "3 is even: %s" % str(proxy.is_even(3))
print "100 is even: %s" % str(proxy.is_even(100))
import xmlrpclib
from SimpleXMLRPCServer import SimpleXMLRPCServer
def is_even(n):
return n%2 == 0
server = SimpleXMLRPCServer(("localhost", 8000))
print "Listening on port 8000..."
server.register_function(is_even, "is_even")
server.serve_forever()