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.
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).
I wrote the following to document for myself the difference between using classmethod and staticmethod.
”’Class and static methods can be called with or without an instance
Static methods refer to the object attributes of the defining class”’
class StatusQuo(object):
status = ‘no change’
def StaticStatus():
return StatusQuo.status
StaticStatus=staticmethod(StaticStatus)
def ClassStatus(cls):
return cls.status
ClassStatus=classmethod(ClassStatus)
class StatusDerived(StatusQuo):
status = ‘dynamic’
@classmethod
def ClsStatus(cls):
return cls.status
@staticmethod
def StatStatus():
return StatusDerived.status
print ‘Class method StatusQuo.ClassStatus():’, \
StatusQuo.ClassStatus()
print ‘Static method StatusQuo.StaticStatus():’, \
StatusQuo.StaticStatus()
s = StatusQuo()
print ‘Class method s.ClassStatus():’, s.ClassStatus()
print ‘Static method s.StaticStatus():’, s.StaticStatus()
print ‘Class method StatusDerived.ClassStatus():’, \
StatusDerived.ClassStatus()
print ‘Static method StatusDerived.StaticStatus():’, \
StatusDerived.StaticStatus()
d = StatusDerived()
print ‘Class method d.ClassStatus():’, d.ClassStatus()
print ‘Static method d.StaticStatus():’, d.StaticStatus()
print ‘Class method StatusDerived.ClsStatus():’, \
StatusDerived.ClsStatus()
print ‘Static method StatusDerived.StatStatus():’, \
StatusDerived.StatStatus()
print ‘Class method d.ClsStatus():’, d.ClsStatus()
print ‘Static method d.StatStatus():’,d.StatStatus()