My first thread fails to start.

This is the place for queries that don't fit in any of the other categories.

My first thread fails to start.

Postby RainbowShibe » Wed Jan 01, 2014 1:14 am

I was recently improving my IRC bot when i came accross this problem, my thread for my botScript.bot() wont start, i have tried seeing weither a connection cannot be put through a thread, but my botScript.loop() works fine.

Heres the code:

test.py:

Code: Select all
#!/usr/bin/python
import threading
import botScript
import socket
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
greetingpost = raw_input("greeting post: ")
nick2 = raw_input("nick: ")
server2 = raw_input("server: ")
if server2 == None or "":
   server2 = "webchat2.irchighway.net"
joininstantly = raw_input("Join instantly? yes or no: ")
irc.connect((server2, 6667))
channel2 = None
password2 = raw_input("password: ")
owner2 = raw_input("owner: ")
connection2 = irc
deftimeout2 = socket.getdefaulttimeout()
deftimeoutstr2 = str(deftimeout2)
if deftimeout2 == None:
   deftimeoutstr2 = "None"
print("The default timeout is: %s" % (deftimeoutstr2))
newtimeout2 = "A new timeout has not been set"
host2 = "None"
irc.send('USER %s %s %s :bot\r\n' % (nick2, host2, server2))
irc.send('NICK %s\n' % (nick2))
irc.send('PRIVMSG %s :im online\r\n' % (owner2))
irc.send('PRIVMSG NICKSERV :IDENTIFY %s\r\n' % (password2))
if joininstantly == "yes":
   channel2 = raw_input("channel: #")
   channel2 = "#%s" % (channel2)
   irc.send("JOIN %s\r\n" % (channel2))
   irc.send("PRIVMSG %s :%s\r\n" % (channel2, str(greetingpost)))
class threads (threading.Thread):
   def __init__(self, threadID, name, section, nick, server, channel, password, owner, connection, deftimeout, deftimeoutstr, newtimeout, host):
      threading.Thread.__init__(self)
      self.threadID = threadID
      self.name = name
      self.nick = nick
      self.server = server
      self.channel = channel
      self.password = password
      self.owner = owner
      self.connection = connection
      self.deftimeout = deftimeout
      self.deftimeoutstr = deftimeoutstr
      self.newtimeout = newtimeout
      self.host = host
      self.section = section
   def run(self):
      if self.section == 1:
         try:
            print("starting botScript.loop")
            botScript.loop(self.connection)
         except:
            print("failed botScript.loop")
      if self.section == 2:
         try:
            print("starting botScript.bot")
            botScript.bot(self.nick, self.server, self.channel, self.password, self.owner, self.connecton, self.deftimeout, self.deftimeoutstr, self.newtimeout, self.host)
         except:
            print("failed botScript.bot")
thread1 = threads(1, "Thread-1", 1, nick2, server2, channel2, password2, owner2, connection2, deftimeout2, deftimeoutstr2, newtimeout2, host2)
thread2 = threads(1, "Thread-1", 2, nick2, server2, channel2, password2, owner2, connection2, deftimeout2, deftimeoutstr2, newtimeout2, host2)

thread1.start()
thread2.start()


And the module botScript.py:

Code: Select all
#!/usr/bin/python
import socket
import sys
import string
import random
import time
import os
def bot(nick, server, channel, password, owner, connection, deftimeout, deftimeoutstr, newtimeout, host):
   irc = connection
   open = os.open('/home/alucero565/MyFiles/python/bots/help', os.O_RDONLY)
   file = str(os.stat('/home/alucero565/MyFiles/python/bots/help').st_size)
   file = file.strip('L')
   file = int(file)
   file = os.read(open, file)
   help = str(file)
   connected = True
   while 1:
      recieve=irc.recv(1024)
      print(recieve)
      if recieve.find(':hello '+nick) !=-1:
         c=recieve.split(':hello '+nick)
         r=c[1].strip()
         irc.send('PRIVMSG '+channel+' :hello\r\n')
      if recieve.find(nick+' :!disconnect 1597 ') !=-1:
         c=recieve.split(' :!disconnect 1597 ')
         a=c[1].strip()
         irc.send('QUIT :'+a+'\r\n')
         logopen.close()
         break
      if recieve.find(':!say ') !=-1:
         c=recieve.split(':!say ')
         a=c[1].strip()
         irc.send('PRIVMSG '+channel+' :'+a+'\r\n')
      if recieve.find(nick+' :!join ') !=-1:
         c=recieve.split(nick+' :!join ')
         a=c[1].strip()
         channel=str(a)
         irc.send(' JOIN '+a+'\r\n')
      if recieve.find(':!part ') !=-1:
         c=recieve.split(':!part ')
         a=c[1].strip()
         irc.send('PART '+a+'\r\n')
      if recieve.find(':!help') !=-1:
         irc.send('PRIVMSG '+channel+help)
      if recieve.find(':!nick '+nick+' ') !=-1:
         c=recieve.split(':!nick '+nick+' ')
         a=c[1].strip()
         nick=str(a)
         irc.send('NICK '+a+'\n')
      if recieve.find(':!me ') !=-1:
         c=recieve.split(':!me ')
         a=c[1].strip()
         irc.send('PRIVMSG '+channel+' :\001ACTION '+a+'\001\r\n')
      if recieve.find(':!deftimeout.read') !=-1:
         irc.send('PRIVMSG '+channel+' :'+deftimeoutstr+'\r\n')
      if recieve.find(':!deftimeout.set ') !=-1:
         c=recieve.split(':!deftimeout.set ')
         a=c[1].strip()
         if a!='None':
            if a!='none':
               t=float(c[1].strip())
               socket.setdefaulttimeout(t)
               deftimeout=(t)
               irc.send('PRIVMSG '+channel+' :The new default timeout is: '+str(t)+'\r\n')
               print('default timeout: '+str(t))
               deftimeoutstr=str(t)
         elif a=='None':
            irc.send('PRIVMSG '+channel+' :Cannot set to None\r\n')
         elif a=='none':
            irc.send('PRIVMSG '+channel+' :Cannot set to none\r\n')
      if recieve.find(':!deftimeout.reset') !=-1:
         socket.setdefaulttimeout(None)
         irc.send('PRIVMSG '+channel+' :Default Timeout: None\r\n')
      if recieve.find(':!commands.deftimeout') !=-1:
         irc.send('PRIVMSG '+channel+' :!deftimeout::   !deftimeout.read: reads the current default timeout   !deftimeout.set: sets a new default timeout (only use numbers)   !deftimeout.reset: resets the default timeout to infinite (aka None)\r\n')
      if recieve.find(channel) !=-1:
         value=random.randrange(1, 100)
         if value==50:
            irc.send('PRIVMSG '+channel+' :hello\r\n')
      if recieve.find(':!roll') !=-1:
         c=recieve.split(':!roll')
         a=c[1].strip()
         if a.find('.sides') !=-1:
            sides=a.split('.sides')
            amountofsides=float(sides[1].strip())
            roll=str(random.randrange(1, amountofsides))
            irc.send('PRIVMSG '+channel+' :'+roll+'\r\n')
         if a.find('.reg') !=-1:
            roll=str(random.randrange(1, 6))
            irc.send('PRIVMSG '+channel+' :'+roll+'\r\n')
      if recieve.find(':!identify ') !=-1:
         c=recieve.split(':!identify ')
         r=c[1].strip()
         irc.send('PRIVMSG NickServ :IDENTIFY '+r+'\r\n')
      if recieve.find(':!reset') !=-1:
         irc.send('PRIVMSG '+channel+' :this command is still in beta\r\n')
def loop(connection):
   irc = connection
   while 1:
      recieve = irc.recv(1024)
      if recieve.find("PING ") !=-1:
         message2 = recieve
         message1 = recieve.split()[1]
         irc.send("PONG %s\r\n" % (message1))
         print(message2)
         print("PONG %s" % (message1))
Last edited by stranac on Wed Jan 01, 2014 1:33 am, edited 1 time in total.
Reason: First post lock.
MLP:FIM <3
Image
User avatar
RainbowShibe
 
Posts: 10
Joined: Wed Jan 01, 2014 12:39 am
Location: Somewhere Over The Rainbow

Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], micseydel and 2 guests

cron