Socket issue when connecting to IRC

By : Jorge
Source: Stackoverflow.com
Question!

I am trying create a very simple python bot that joins a specific channel, and says a random phrase every time someone says anything. I am doing this primarily to learn how sockets and IRC works. I am getting the following error message:

    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

When running the following code:

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

I believe the issue is with:

irc.connect((irc_server, 6667))

My goal is to connect to the IRC server using a proxy loaded from the proxy list. Any ideas?

By : Jorge


Answers

These lines:

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

will result in port being a string containing the required port number. An integer is required for the port. Change your code to :

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

which converts the port from a string into an integer as required.

By : mhawke


You are determining the proxy port with

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

You are then passing it to socks:

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

But it's still a string. I bet it will work if you change the latter to

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

It's not actually used until you call irc.connect but it's the port from the setproxy.



This video can help you solving your question :)
By: admin