Monthly Archives: September 2008

Quick Python Reference

Python support for Internet Protocols

The documentation for the packages for Internet Protocol handling can be found at Internet Protocols and Support

  • urllib: This module provides a high-level interface for fetching data across the World Wide Web
  • urllib2: efines functions and classes which help in opening URLs (mostly HTTP) in a complex world — basic and digest authentication, redirections, cookies and more.
  • httplib: defines classes which implement the client side of the HTTP and HTTPS protocols. It is normally not used directly — the module urllib uses it to handle URLs that use HTTP and HTTPS.
  • urlparse: defines a standard interface to break URL strings up in components (addressing scheme, network location, path etc.), to combine the components back into a URL string, and to convert a “relative URL” to an absolute URL given a “base URL.”
  • cookielib: defines classes for automatic handling of HTTP cookies. It is useful for accessing web sites that require small pieces of data – cookies – to be set on the client machine by an HTTP response from a web server, and then returned to the server in later HTTP requests.
  • Cookie: defines classes for abstracting the concept of cookies, an HTTP state management mechanism. It supports both simple string-only cookies, and provides an abstraction for having any serializable data-type as cookie value.
  • uuid: provides immutable UUID objects (the UUID class) and the functions uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5 UUIDs as specified in RFC 4122.

urllib2 tricks

As far as I can tell urllib2 supports by default only GET and POST requests (ref). In order to be able to generate the other types of requests (PUT, DELETE, OPTION) I think you’ll need to extend urllib2.Request and override the get_method() method to return the type of request you want to make.

Special method names

A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names.This is Python’s approach to operator overloading, allowing classes to define their own behavior with respect to language operators.

Special method names

The How-To Guide for Descriptors defines descriptors, summarizes the protocol, and shows how descriptors are called.

classmethod and staticmethod

It’s still not very clear what is the difference between the @classmethod and @staticmethod (except the first parameter accepted by the annotated method — for @classmethod it is the class).

Future versions

1 Comment

Filed under links, technolog