Search code examples
pythonpython-2.7websocketsocks

how to get proxy ip and port from text file


how to get proxy ip and port from text file

Code :

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

Error Here :

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")

i need it like this to work :

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

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


Solution

  • You need to convert PORT to an int:

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

    Note that this will raise a ValueError if for some whatever reason PORT can't be converted, so you may want to catch it.

    Depending on the structure of your file, it is most likely that PORT will include a '\n' in the end. You will need to get rid of it with strip before trying to convert it to an int.

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