3

I use Python 2.7.2 and Mechanize 0.2.5.
When I access the Internet, I have to go through a proxy server. I wrote the following codes, but an URLError occurred at the last line.. Does anyone have any solution about this?

import mechanize

br = mechanize.Browser()
br.set_debug_http(True)
br.set_handle_robots(False)

br.set_proxies({
    "http"  : "192.168.20.130:8080",
    "https" : "192.168.20.130:8080",})
br.add_proxy_password("username", "password")

br.open("http://www.google.co.jp/")  # OK
br.open("https://www.google.co.jp/") # Proxy Authentication Required
yutaka2487
  • 1,926
  • 2
  • 13
  • 12

1 Answers1

3

I don't recommend you to use Mechanize, it's outdated. Take a look at requests it will make your life a lot easier. Using proxies with requests it's just this:

import requests

proxies = {
  "http": "10.10.1.10:3128",
  "https": "10.10.1.10:1080",
}

requests.get("http://example.org", proxies=proxies)
scripts
  • 1,452
  • 1
  • 19
  • 24
  • Thank you very much. I don't know requests module. I'm trying it now. How can I specify username and password for Proxy Authentication? – yutaka2487 Nov 22 '12 at 03:34
  • you need to use the proxy url like: username:mypassword@10.10.1.10:3128 – scripts Nov 22 '12 at 03:37
  • 3
    Thank you. Surely your way is accepted by basic authentication. When proxy server requires digest auth, username and password cannot be embedded in proxy-url. I tried requests.auth.HTTPProxyAuth, but proxy returned 407. – yutaka2487 Nov 22 '12 at 04:08