Hema Hema - 1 year ago 946
Python Question

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)
# ----------------- ++
PROXY = P[nm]
# ----------------- ++


Error Here :

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

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

i need it like this to work :

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


Answer Source

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.

    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, IP, int(PORT.strip()))
except ValueError:
    print('Illegal port')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download