0

I am trying to set an http proxy when creating a mechanize broswer instance but it doesn't seem to be working at all or throwing any kind of errors.

from mechanize import Browser
br = Browser()
ua = 'Mozilla/5.0 (X11; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/1    8.0 (compatible;)'
br.addheaders = [('User-Agent', ua), ('Accept', '*/*')]
br.set_proxies({'http':'116.226.11.254:8118'})
br.open("https://xxx.xxx")

This code still opens the url with local ip and doesn't use the proxy at all. If proxy were unreachable then it should have thrown such error but its not happening. I tried to put in some invalid ip for proxy like

br.set_proxies({'http':'116.22as6.11.25as4:8118'})

but this code doesnt throw any error too!! Is is possible that some other inbuild python code is re-writing proxy? I am using python 2.7.10 inside a virtual environment with latest mechanize, and trying to open an https address with this code

I have tried this but it still ignores proxy

Community
  • 1
  • 1
MohitC
  • 4,541
  • 2
  • 34
  • 55

3 Answers3

0

Note that you are setting proxy just for the 'http' protocol, but call 'https' in br.open.

Try replacing the protocol to 'https' in the br.set_proxies.

ygilad
  • 1
0

after hours of researching I have realised that this function of mechanize is useless, so I found another method to make this work with mechanize requests:

#!/usr/bin/python
import mechanize
import socks
import socket

# This will be printing your real ip
print mechanize.Browser().open("https://api.ipify.org/?format=raw").read()
ip = "0.0.0.0"
port = 1234
socks.setdefaultproxy(socks.PROXY_TYPE_HTTP, ip, port)
socket.socket = socks.socksocket

br = mechanize.Browser()
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.addheaders = [("User-agent", "Mozilla/5.0")]
# Then with your proxy:
print br.open("https://api.ipify.org/?format=raw").read()
# Will be printing the ip from the proxy

And then this REALLY works for me. I hope this work for you!

P.Pro
  • 1
  • 1
0
br.set_handled_schemes(['https', 'http'])

Did it for me.

zlr
  • 789
  • 11
  • 22