簡體   English   中英

在python中測試HTTPS代理

[英]Test an HTTPS proxy in python

我管理了很多HTTPS代理(那些代理有自己的SSL連接)。 我正在python中構建一個診斷工具,試圖通過每個代理連接到一個頁面,如果無法通過其中一個代理連接,則給我發電子郵件。

我打算這樣做的方法是使用urllib連接每個代理並返回一個頁面,該頁面應該用下面的代碼說“成功”。

def fetch(url):
    connection = urllib.urlopen(
    url,
    proxies={'http':"https://"+server+':443'}
    )
    return connection.read()


print fetch(testURL)

這將獲取我想要的頁面完全問題是它仍然會獲取我想要的頁面,即使代理服務器信息不正確或代理服務器處於非活動狀態。 因此要么它從不使用代理服務器,要么它嘗試它並在它失敗時沒有它連接。

我怎么能糾正這個?

編輯:似乎沒有人知道如何做到這一點。 我將開始閱讀其他語言庫,看看他們是否能更好地處理它。 有沒有人知道在Go等其他語言中是否更容易?

編輯:我剛剛在下面的評論中寫了這個,但我認為這可能是一個誤解。 “代理有它自己的ssl連接。所以,如果我去google.com,我首先與foo.com進行密鑰交換,然后另一個與目的地址bar.com或目的地地址baz.com進行密鑰交換目的地不必須是https,代理是https“

大多數人都將https代理理解為理解CONNECT請求的代理。 我的例子創建了直接的ssl連接。

try:
    import http.client as httplib # for python 3.2+
except ImportError:
    import httplib # for python 2.7


con = httplib.HTTPSConnection('proxy', 443) # create proxy connection
# download http://example.com/ through proxy
con.putrequest('GET', 'http://example.com/', skip_host=True)
con.putheader('Host', 'example.com')
con.endheaders()
res = con.getresponse()
print(res.read())

如果您的代理是反向代理,則更改

con.putrequest('GET', 'http://example.com/', skip_host=True)

con.putrequest('GET', '/', skip_host=True)`

我認為它不適用於https請求。 它是否正確? 如果是,那么上面的代碼僅為http定義代理。 嘗試將其添加到https:

proxies={'https':"https://"+server+':443'}

另一個選擇是使用requests python模塊而不是urllib 看看http://docs.python-requests.org/en/latest/user/advanced/#proxies

從讀取代碼開始,urllib似乎不支持這一點,並且不清楚urllib2是否支持。 但是,如果只使用curl(或curllib),那通常是最常見的HTTP客戶端api(雖然更復雜,這就是為什么urllib等出現的原因)。

看一下命令行curl工具,似乎很有希望:

   -x, --proxy <[protocol://][user:password@]proxyhost[:port]>
          Use the specified HTTP proxy. If the port number is not specified, it is assumed at port 1080.

          This  option  overrides  existing environment variables that set the proxy to use. If there's an environment variable setting a proxy, you can set
          proxy to "" to override it.

          All operations that are performed over an HTTP proxy will transparently be converted to HTTP. It means that certain protocol  specific  operations
          might not be available. This is not the case if you can tunnel through the proxy, as one with the -p, --proxytunnel option.

          User  and  password that might be provided in the proxy string are URL decoded by curl. This allows you to pass in special characters such as @ by
          using %40 or pass in a colon with %3a.

          The proxy host can be specified the exact same way as the proxy environment variables, including the protocol prefix (http://)  and  the  embedded
          user + password.

          From  7.21.7,  the  proxy  string  may  be  specified with a protocol:// prefix to specify alternative proxy protocols. Use socks4://, socks4a://,
          socks5:// or socks5h:// to request the specific SOCKS version to be used. No protocol specified, http:// and all others will be  treated  as  HTTP
          proxies.

          If this option is used several times, the last one will be used.

使用超時怎么樣? 如果代理在30秒內未能連接,則應注意未連接。

def fetch(url, server):
 proxy_handler = urllib2.ProxyHandler({'http':'https://'+server+':443'})
 opener = urllib2.build_opener(proxy_handler, urllib2.HTTPHandler(debuglevel=0))
 urllib2.install_opener(opener)

 try:
  response = opener.open( url, timeout = 30)
  return response.read()
 except:
  print "Can't connect with proxy %s" % (server)

print fetch(url,serverIp)

您可以更改debuglevel = 1以查看連接詳細信息

我將它用於全局代理,並且我的互聯網連接30秒是最大超時,以了解我是否連接。 在我的測試中,如果連接時間超過30秒,則總是失敗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM