D
D
Daulet2015-07-24 21:47:27
Python
Daulet, 2015-07-24 21:47:27

How to check if the proxy is working through which I want to send requests, python?

I need to start Selenium by writing that it will work through a proxy - webdriver.Firefox(proxy=self.proxy), but if the proxy is not working, then the browser is endlessly loaded. How can I check if the proxy is working using Python? (os windows)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aLLyofgod, 2015-07-24
@Danil38

It is possible so;

import urllib
import socket

socket.setdefaulttimeout(2) # timeout. increase for slow proxies

try:
    urllib.urlopen(
        "http://ya.ru",
        proxies={'http':'http://example.com:8080'} # your proxy here
    )
except IOError:
    print "Bad proxy!" # do it if proxy connect is failed
else:
    print "All was OK" # do it if all ok

Play around with timeouts. Depending on what proxies you have.

S
stayHARD, 2015-07-24
@stayHARD

try:
...
except ошибка, которую вы отлавливаете:
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question