1br = mechanize.Browser()
2# Explicitly configure proxies (Browser will attempt to set good defaults).
3# Note the userinfo ("joe:password@") and port number (":3128") are optional.
4br.set_proxies({"http": "joe:password@myproxy.example.com:3128",
5 "ftp": "proxy.example.com",
6 })
7# Add HTTP Basic/Digest auth username and password for HTTP proxy access.
8# (equivalent to using "joe:password@..." form above)
9br.add_proxy_password("joe", "password")
10# Add HTTP Basic/Digest auth username and password for website access.
11br.add_password("http://example.com/protected/", "joe", "password")
12# Add an extra header to all outgoing requests, you can also
13# re-order or remove headers in this function.
14br.finalize_request_headers = lambda request, headers: headers.__setitem__(
15 'My-Custom-Header', 'Something')
16# Don't handle HTTP-EQUIV headers (HTTP headers embedded in HTML).
17br.set_handle_equiv(False)
18# Ignore robots.txt. Do not do this without thought and consideration.
19br.set_handle_robots(False)
20# Don't add Referer (sic) header
21br.set_handle_referer(False)
22# Don't handle Refresh redirections
23br.set_handle_refresh(False)
24# Don't handle cookies
25br.set_cookiejar()
26# Supply your own mechanize.CookieJar (NOTE: cookie handling is ON by
27# default: no need to do this unless you have some reason to use a
28# particular cookiejar)
29br.set_cookiejar(cj)
30# Tell the browser to send the Accept-Encoding: gzip header to the server
31# to indicate it supports gzip Content-Encoding
32br.set_request_gzip(True)
33# Do not verify SSL certificates
34import ssl
35br.set_ca_data(context=ssl._create_unverified_context(cert_reqs=ssl.CERT_NONE))
36# Log information about HTTP redirects and Refreshes.
37br.set_debug_redirects(True)
38# Log HTTP response bodies (i.e. the HTML, most of the time).
39br.set_debug_responses(True)
40# Print HTTP headers.
41br.set_debug_http(True)
42
43# To make sure you're seeing all debug output:
44logger = logging.getLogger("mechanize")
45logger.addHandler(logging.StreamHandler(sys.stdout))
46logger.setLevel(logging.INFO)
47
48# Sometimes it's useful to process bad headers or bad HTML:
49response = br.response() # this is a copy of response
50headers = response.info() # this is a HTTPMessage
51headers["Content-type"] = "text/html; charset=utf-8"
52response.set_data(response.get_data().replace("<!---", "<!--"))
53br.set_response(response)
54