2016-09-28 81 views
0

我正在嘗試創建一個非常簡單的加入特定頻道的python機器人,並且每次有人說任何事時都會說出一個隨機短語。我這樣做主要是爲了瞭解套接字和IRC如何工作。我收到以下錯誤消息:當連接到IRC時發生套接字問題

Traceback (most recent call last): 
    File "D:/Users/Administrator/Documents/Classwork/Dropbox/Bots/Rizon Bot/ViewBot.py", line 36, in <module> 
    irc.connect((irc_server, 6667)) 
    File "D:\Users\Administrator\Documents\Classwork\Dropbox\Bots\Rizon Bot\socks.py", line 351, in connect 
    _orgsocket.connect(self,(self.__proxy[1],portnum)) 
    File "C:\Python27\lib\socket.py", line 228, in meth 
    return getattr(self._sock,name)(*args) 
TypeError: an integer is required 

Process finished with exit code 1 

當運行下面的代碼:

import random 
from urlparse import urlparse 
import socks 
import socket 
import time 

#---- Definitions ----# 
#Proxylist 
proxy_list = [] 
with open("proxy_list.txt") as f: 
    proxy_list = f.readlines() 

#IRC Name List 
irc_names = [] 
with open("irc_names.txt") as f: 
    irc_names = f.readlines() 

#Phrase List 
phrase_list = [] 
with open("phrase_list.txt") as f: 
    phrase_list = f.readlines() 

irc_server = "irc.rizon.net" 
irc_port = 6667 
irc_channel = '#test12' # Change this to what ever channel you would like 


queue = 0 
#---- Code ----# 
proxy = random.choice(proxy_list) 
hostname = proxy.split(":")[0] 
port = proxy.split(":")[1] 
#irc = socket.socket (socket.AF_INET, socket.SOCK_STREAM) 
irc = socks.socksocket() 
irc.setproxy(socks.PROXY_TYPE_HTTP, hostname, port) 
irc.connect((irc_server, 6667)) 
print irc.recv (4096) 
irc.send ('NICK ' + random.choice(irc_names) + '\r\n') 
irc.send ('USER botty botty botty :Sup\r\n') 
irc.send ('JOIN ' + irc_channel + '\r\n') 
time.sleep(4) 
irc.send('PRIVMSG ' + irc_channel + ' :' + random.choice(phrase_list) + '\r\n') 
while True: 
    data = irc.recv (4096) 
    print data 
    if data.find('KICK') != -1: 
     irc.send('JOIN '+ irc_channel + '\r\n') 
    if data.find('') != -1: # !test command 
     irc.send('PRIVMSG ' + irc_channel + ' :' + (random.choice(phrase_list)) + '\r\n') 

我相信這個問題是:

irc.connect((irc_server, 6667)) 

我的目標是要連接到IRC服務器使用從代理列表中加載的代理。有任何想法嗎?

+1

請將完整的追溯添加到您的問題。 – mhawke

+0

@mhawke完成!謝謝! – Jorge

回答

1

您確定代理端口

port = proxy.split(":")[1] 

然後您可以將它傳遞給襪子:

irc.setproxy(socks.PROXY_TYPE_HTTP, hostname, port) 

但它仍然是一個字符串。我敢打賭,如果你改變了後者

irc.setproxy(socks.PROXY_TYPE_HTTP, hostname, int(port)) 

它實際上並不使用直到調用irc.connect但它是從setproxy港口它將工作。

+0

非常感謝! – Jorge

1

這些行:

port = proxy.split(":")[1] 
irc.setproxy(socks.PROXY_TYPE_HTTP, hostname, port) 

將導致端口是一個包含所需端口號。該端口需要integer。將您的代碼更改爲:

port = int(proxy.split(":")[1]) 
irc.setproxy(socks.PROXY_TYPE_HTTP, hostname, port) 

將端口從字符串轉換爲所需的整數。

+0

非常感謝! – Jorge