views:

74

answers:

2

I've got mechanize setup and working with python. I am adding support for using a proxy, but how do I check that I am actually using the proxy?

Here is some code I am using:

ip = 'some proxy ip address'
br.set_proxies({"http://": ip} )

I started to wonder if it was working because just to do some testing I typed in:

ip = 'asdfasdf'

and it didn't throw an error. So how do I go about checking if it is really using the ip address for the proxy that I pass in or the ip address of my computer? Is there a way to return info on your ip in mechanize?

A: 

I am not sure how to handle this issue with mechanize, but you could read the next link that explains how to do it without mechanize (but still in python):
http://stackoverflow.com/questions/765305/proxy-check-in-python
The simple solution provided at the above-mentioned link could be easily adapted to your needs.
Thus, instead of the line:
print "Connection error! (Check proxy)"
you could replace by
SucceededYesNo="NO"
and instead of
print "All was fine"
just replace by
SucceededYesNo="YES"
Now, you have a variable available for further processing.
I am however afraid this will not cover the cases when the target web page is down because the same error might occur out of two causes (so one would not know whether a NO outcome is coming from a not working proxy server or from a bad web page), but still could be a solution: what about to check with the above-mentioned code a working web page? i.e. www.google.com? In this way, you could eliminate one cause and it remains the other.

carmao
A: 

maybe like this ?

br = mechanize.Browser()
br.set_proxies({"http": '127.0.0.1:80'})

you need to debug for more information

br.set_debug_http(True)
br.set_debug_redirects(True)
Gunslinger_