Convenient user-agent class.
Do not use .add_handler() to add a handler for something already dealt with by this code.
Public attributes:
addheaders: list of (name, value) pairs specifying headers to send with every request, unless they are overridden in the Request instance.
>>> ua = UserAgent() >>> ua.addheaders = [ ... ("User-agent", "Mozilla/5.0 (compatible)"), ... ("From", "responsible.person@example.com")]
There are no implemented interfaces.
default_features
(type:
list
)
['_redirect', '_cookies', '_referer', '_refresh', '_equiv', '_basicauth', '_digestauth', '_proxy', '_proxy_basicauth', '_proxy_digestauth', '_seek', '_robots']
default_others
(type:
list
)
['_unknown', '_http_error', '_http_request_upgrade', '_http_default_error']
default_schemes
(type:
list
)
['http', 'ftp', 'file', 'gopher', 'https']
handler_classes
(type:
dict
)
{'_gzip': <class mechanize._gzip.HTTPGzipProcessor at 0x41692c8c>, 'gopher': <class urllib2.GopherHandler at 0x406f02fc>, '_http_request_upgrade': <class mechanize._urllib2_support.HTTPRequestUpgradeProcessor at 0x416a477c>, 'file': <class urllib2.FileHandler at 0x406f026c>, '_cookies': <class mechanize._urllib2_support.HTTPCookieProcessor at 0x416a4f8c>, '_digestauth': <class mechanize._auth.HTTPDigestAuthHandler at 0x4169c65c>, 'ftp': <class urllib2.FTPHandler at 0x406f029c>, '_debug_redirect': <class mechanize._urllib2_support.HTTPRedirectDebugProcessor at 0x416ab0ec>, '_unknown': <class urllib2.UnknownHandler at 0x406f023c>, '_debug_response_body': <class mechanize._urllib2_support.HTTPResponseDebugProcessor at 0x416ab0bc>, 'https': <class mechanize._urllib2_support.HTTPSHandler at 0x416ab1dc>, '_proxy_basicauth': <class mechanize._auth.ProxyBasicAuthHandler at 0x4169c5fc>, '_proxy_digestauth': <class mechanize._auth.ProxyDigestAuthHandler at 0x4169c68c>, 'http': <class mechanize._urllib2_support.HTTPHandler at 0x416ab1ac>, '_proxy': <class mechanize._auth.ProxyHandler at 0x4169c47c>, '_equiv': <class mechanize._urllib2_support.HTTPEquivProcessor at 0x416a4f2c>, '_refresh': <class mechanize._urllib2_support.HTTPRefreshProcessor at 0x416ab11c>, '_seek': <class mechanize._urllib2_support.SeekableProcessor at 0x416a4f5c>, '_referer': <class mechanize._useragent.HTTPRefererProcessor at 0x41692cbc>, '_robots': <class mechanize._urllib2_support.HTTPRobotRulesProcessor at 0x416ab05c>, '_redirect': <class mechanize._urllib2_support.HTTPRedirectHandler at 0x416a414c>, '_http_default_error': <class urllib2.HTTPDefaultErrorHandler at 0x406e2ecc>, '_http_error': <class mechanize._urllib2_support.HTTPErrorProcessor at 0x416ab14c>, '_basicauth': <class mechanize._auth.HTTPBasicAuthHandler at 0x4169c5cc>}
add_handler(handler)
add_password(url, user, password, realm=None)
add_proxy_password(user, password, hostport=None, realm=None)
close()
error(proto, *args)
open(fullurl, data=None)
retrieve(fullurl, filename=None, reporthook=None, data=None)
Returns (filename, headers).
For remote objects, the default filename will refer to a temporary file.
set_cookiejar(cookiejar)
Set a mechanize.CookieJar, or None.
set_debug_http(handle)
Print HTTP headers to sys.stdout.
set_debug_redirects(handle)
Log information about HTTP redirects (including refreshes).
Logging is performed using module logging. The logger name is "mechanize.http_redirects". To actually print some debug output, eg:
import sys, logging logger = logging.getLogger("mechanize.http_redirects") logger.addHandler(logging.StreamHandler(sys.stdout)) logger.setLevel(logging.INFO)
Other logger names relevant to this module:
"mechanize.http_responses" "mechanize.cookies" (or "cookielib" if running Python 2.4)
To turn on everything:
import sys, logging logger = logging.getLogger("mechanize") logger.addHandler(logging.StreamHandler(sys.stdout)) logger.setLevel(logging.INFO)
set_debug_responses(handle)
Log HTTP response bodies.
See docstring for .set_debug_redirects() for details of logging.
set_handle_equiv(handle, head_parser_class=None)
Set whether to treat HTML http-equiv headers like HTTP headers.
Response objects will be .seek()able if this is set.
set_handle_gzip(handle)
Handle gzip transfer encoding.
set_handle_redirect(handle)
Set whether to handle HTTP 30x redirections.
set_handle_referer(handle)
Set whether to add Referer header to each request.
This base class does not implement this feature (so don't turn this on if you're using this base class directly), but the subclass mechanize.Browser does.
set_handle_refresh(handle, max_time=None, honor_time=True)
Set whether to handle HTTP Refresh headers.
set_handle_robots(handle)
Set whether to observe rules from robots.txt.
set_handled_schemes(schemes)
Set sequence of URL scheme (protocol) strings.
For example: ua.set_handled_schemes(["http", "ftp"])
If this fails (with ValueError) because you've passed an unknown scheme, the set of handled schemes will not be changed.
set_password_manager(password_manager)
Set a mechanize.HTTPPasswordMgrWithDefaultRealm, or None.
set_proxies(proxies)
Set a dictionary mapping URL scheme to proxy specification, or None.
set_proxy_password_manager(password_manager)
Set a mechanize.HTTPProxyPasswordMgr, or None.