【问题标题】:how to get proxy ip and port from text file如何从文本文件中获取代理 IP 和端口
【发布时间】:2016-08-16 13:49:55
【问题描述】:

如何从文本文件中获取代理ip和端口

代码:

import re , urllib , urllib2 , socks , socket
proxys  = open('IPs.txt', 'r')
links  = open('URs.txt', 'r')
#----
P = proxys.readlines()
E = links.readlines()
#----
nm = 0
#----
PROXY = P[nm]
#----
for links in E :
 Post = 'id='+ links
 cj = CookieJar()
 #----
 IP,PORT = PROXY.split(":")
 socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, IP, PORT)
 socket.socket = socks.socksocket
 #----
 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
 request = urllib2.Request('https://google.com/', Post)
 # ----------------- ++
 nm+=1
 PROXY = P[nm] 
# ----------------- ++

IPs.txt:

96.47.156.166:10200
96.47.88.7:14328
97.88.243.210:24598
98.201.217.101:23320

此处出错:

PROXY = P[0] # = 96.47.156.166:10200 #from the text file 
IP,PORT = PROXY.split(":")

 socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "96.47.156.166", "10200")

我需要这样才能工作:

 socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "96.47.156.166", 10200) #withot ""

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

【问题讨论】:

    标签: python python-2.7 websocket socks


    【解决方案1】:

    您需要将PORT 转换为int

    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, IP, int(PORT))
    

    请注意,如果由于某种原因无法转换 PORT,这将引发 ValueError,因此您可能想抓住它。

    根据文件的结构,PORT 很可能最后会包含'\n'。在尝试将其转换为 int 之前,您需要使用 strip 删除它。

    try:
        socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, IP, int(PORT.strip()))
    except ValueError:
        print('Illegal port')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-18
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 2014-12-26
      • 2020-02-04
      • 2013-10-28
      • 1970-01-01
      相关资源
      最近更新 更多