通过带有套接字库的socks建立连接

时间:2016-07-27 10:37:53

标签: sockets python-3.x socks

我有这个脚本只是使用socks列表并尝试连接到主机。但它总是显示这个错误:

  File "*.py", line 38, in requestsocks
    s.connect((host, port))
  File "/home/*/.local/lib/python3.5/site-packages/socks.py", line 729, in connect
    _BaseSocket.connect(self, proxy_addr)
TypeError: an integer is required (got type str)

以下是代码:

import socket,socks,random

def checkurl():
    global url
    global url2
    global urlport
    url = input("Insert URL: ")
    if url[0]+url[1]+url[2]+url[3] == "www.":
        url = "http://" + url
    elif url[0]+url[1]+url[2]+url[3] == "http":
        pass
    else:
        url = "http://" + url

    try:
        url2 = url.replace("http://", "").replace("https://", "").split('/')[0].split(":")[0]
    except:
        url2 = url.replace("http://", "").replace("https://", "").split('/')[0]

    try:
        urlport = url.replace("http://", "").replace("https://", "").split('/')[0].split(':')[1]
    except:
        urlport = "80"

def proxylist():
    global entries
    out_file = str(input("Enter the proxy list: "))
    entries = open(out_file).readlines()
    requestsocks()

def requestsocks(): 
    while True:
        proxy = random.choice(entries).strip().split(':')
        host = str(url2)
        port = int(urlport)
        socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, proxy[0], proxy[1], True)
        s = socks.socksocket()
        s.connect((host, port))
        s.sendall('Hello world')

checkurl()
proxylist()

我不知道如何解决这个问题。想法? 附:我从这里拿走了袜子清单:https://www.socks-proxy.net/

1 个答案:

答案 0 :(得分:1)

我对套接字库做了很多工作,但它要求主机和端口都有一个整数。尝试获取这样的IP。

添加此------> host = socket.gethostbyname(host)

def requestsocks(): 
    while True:
        proxy = random.choice(entries).strip().split(':')
        host = str(url2)
        # convert url to IP address
        host = socket.gethostbyname(host)
        port = int(urlport)
        socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, proxy[0], proxy[1], True)
        s = socks.socksocket()
        s.connect((host, port))
        s.sendall('Hello world')