连接到IRC时出现套接字问题

时间:2016-09-28 22:07:47

标签: python python-2.7 sockets irc

我正在尝试创建一个非常简单的连接特定频道的python bot,每当有人说什么时都会说一个随机短语。我这样做主要是为了了解套接字和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服务器。有任何想法吗?

2 个答案:

答案 0 :(得分:1)

您正在使用

确定代理端口
irc.setproxy(socks.PROXY_TYPE_HTTP, hostname, int(port))

然后你将它传递给袜子:

irc.connect

但它仍然是一个字符串。我敢打赌,如果你将后者改为

,它会奏效
<form class="form-horizontal" method="post" id="status-update-form">
    <div class="form-group">
        <div class="col-sm-8">
            <textarea class="form-control" rows="2" name="status" id="status" placeholder="Anything special to share?"></textarea>
        </div>
        <div class="col-sm-4">
            <button type="submit" class="btn btn-success btn-block btn-lg" style="padding: 13.4px;">Post</button>
        </div>

在你调用<div class="container"> <nav class="navbar navbar-inverse navbar-fixed-top"> <div class="navbar-header"> 之前,它实际上不是使用,而是来自setproxy的端口。

答案 1 :(得分: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)

根据需要将字符串中的端口转换为整数。