2011-01-24 8 views
0

に接続しません「ポートに接続しています...」それはちょうど言うだろう「ソケットがタイムアウトになりました。」Pythonの:私はそれが</p> <p>をプリントアウトするサーバーに接続できないんだけどサーバー

私のプログラムは明日に予定されています。これは実際にはうまくいくでしょう。

編集コード:ここでは、ポートへの接続のみを使用します。 それ以外は印刷されません。

import socket, string, time, random, re, urllib2, cookielib, smtplib, os 

class Pibot: #main class 
     def __init__(self): #basic information to allow for the rest of the program to work. 
      self.server= 'irc.evilzone.org' 
      self.port = 6667 
      self.botname= 'pibot' 
      self.chan= 'test' 
      self.owner = 'Josh.H' 
      self.nick = "bawt" 
      self.irc = None 
      self.data = '' 

    def iConnect(self): #trys to connect to the server and allows the user to see if it failed to connect. 
      print ("Connecting to ports...") 
      print self.data 
      time.sleep(3) 
      try: 
        self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
        self.irc.connect((self.server, self.port)) 

      except (socket.error, socket.herror, socket.gaierror): 
        print "Failed to connect to Ports" 



    def iStart(self): 
      #Not guaranteed to send all your data, iisn't checking the return values 
      #however this function iStart is used to send the NICK of the bot and the USER to the server through particle data 
      #it then auto joins the channel 
      #in future development I'd like to get accuainted with Twisted or IRCutils as they allow it to be quiet powerful and less buggy 

      self.irc.send('NICK %s\r\n' % self.nick) 
      self.irc.send("USER %s %s bla :%s\r\n" % ("Ohlook", 'itsnotmy', 'Realname')) 
      time.sleep(4) 
      self.irc.send("JOIN #%s\r\n" % self.chan) 
      self.data = self.irc.recv(4096) 

    def MainLoop(self,iParse = 0): #MainLoop is used to make the commands executable ie !google !say etc; 
      try: 
       while True: 
        # This method sends a ping to the server and if it pings it will send a pong back 
        #in other clients they keep receiving till they have a complete line however mine does not as of right now 
        #The PING command is used to test the presence of an active client or 
        #server at the other end of the connection. Servers send a PING 
        #message at regular intervals if no other activity detected coming 
        #from a connection. If a connection fails to respond to a PING 
        #message within a set amount of time, that connection is closed. A 
        #PING message MAY be sent even if the connection is active. 
        #PONG message is a reply to PING message. If parameter <server2> is 
        #given, this message will be forwarded to given target. The <server> 
        #parameter is the name of the entity who has responded to PING message 
        #and generated this message. 

        self.data = self.irc.recv(4096) 
        if self.data.find ('PING') != -1: 
         self.irc.send(("PONG %s \r\n") % (self.recv.split() [ 1 ])) #Possible overflow problem 

        if self.data.find("!google") != -1: 
         #googles the search term and displays the first 5 results 
         #format = !google: <Search Term> 
         #One thing that I noticed is that it will print on a seperate line without the header 
         #In the next Update I would have fixed this. 
         fin = data.split(':') 
         if not fin: 
           irc.send("PRIVMSG #%s :syntax'^google :search term\r\n'" % chan) 

         else: 
          #In the next version to avoid overflow I will create another if statement and edit the search code 
          #However I am using what xgoogle has reccomended. 
           fin = fin[3].strip() 
           gs = GoogleSearch(fin) 
           gs.results_per_page = 5 
           results = gs.get_results() 
           for result in results: 
            irc.send("PRIVMSG #%s :%s\r\n" % (chan, result.url.encode("utf8"))) 

        ############################################################################################################################### 
        # No excpetion checking here, these functions can and will fail in time and in later versions will need to be edited.   
        # If hellboundhackers changes this code may break 
        # This function takes a quote from the header of hellboundhackers 
        # it first looks at the header of the USer agent then the header of the website (HBH) and reads it then prints 
        # the quote when QUOTEM is recognized in the irc closes the connection to the wbesite and deletes the cookie 
        ############################################################################################################################### 

        if "QUOTEM" in self.data: 
         #Pulls a quote from HBH 
         cj = cookielib.CookieJar() 
         opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 
         opener.addheaders.append(('User-agent', 'Mozilla/4.0')) 
         opener.addheaders.append(('Referer', 'http://www.hellboundhackers.org/index.php')) 
         resp = opener.open('http://www.hellboundhackers.org/index.php') 
         r = resp.read() 
         resp.close() 
         del cj, opener 
         da = re.findall("Enter; width:70%;'>(.*)",r) 
         self.irc.send("PRIVMSG #%s :%s\r\n" % (chan, da[0])) # Note Possible overflow 

        if "!whoareyou" in self.data: 
         #bot info allows users on IRC to see which commands are currently working 
         self.irc.send("PRIVMSG #%s :I am %s, I was created By:%s \r\n" % (self.chan, self.nick,self.owner)) 
         self.irc.send("PRIVMSG #%s :I was written in Python 27, and edited with IDLE\r\n" % self.chan) 
         self.irc.send("PRIVMSG #%s :The Classes used are socket, string, time, re, urllib2, cookielib\r\n" % self.chan) 
         self.irc.send("PRIVMSG #%s :As well as some functions from various other sources(supybot,twisted,xgoogle)\r\n" % self.chan) 
         self.irc.send("PRIVMSG #%s :type ^commands for a list of things I can do\r\n" % self.chan) 
      except (socket.error, socket.timeout): 
        print "Sockets timed out." 

bot = Pibot() 
bot.iConnect() 
bot.MainLoop() 

サイドノート:エラーはありません。 非常に感謝しています。また、私はただ学んでいるので、私に火をつけないでください。 :(

EDIT2:私は問題のほとんどを固定しているし、今のエラーを取得しています:

Traceback (most recent call last): 
    File "L:\txtbot.py", line 119, in <module> 
    bot.MainLoop() 
    File "L:\txtbot.py", line 64, in MainLoop 
    self.irc.send(("PONG %s \r\n") % (self.recv.split() [ 1 ])) #Possible overflow problem 
AttributeError: Pibot instance has no attribute 'recv' 
+0

'telnet irc.evilzone.org 6667'を使ってコマンドラインから接続できますか? – Thomas

+0

telnet関数を使用するコード内か、一般的なサーバーを意味しますか? – 21days

答えて

1

それはあなたがソケットに接続情報を渡すことはありませんしているようだ:

   self.irc = socket.socket() 

Iそれはこのようなものであるべきだと思う:iConnect

   self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
       self.irc.connect((self.server, self.port)) 
+0

私はそれを最初に持っていましたが、socket.socketは同じものになります(よく私は多くのircスケルトンでそのようなものが見えました)。 EDIT: IConnectの中: "\のtxtbot.py L"、ライン24、 elf.irc = socket.socket(はsocket.AF_INET、socket.SOCK_STREAM) NameError:グローバル名 ' bot.iConnect() ファイルの行118、 elf 'は定義されていません 自分自身だが他の人がいるからです。 – 21days

+0

@ 21days:あなたが作ったコードに変更を加えて、遠隔で読むことができる質問やどこかに入れてください。 –

+0

"elf"と入力しましたが、 "self"ではありません。あなたは「他者」をどういう意味ですか?そこには1つのエラーメッセージしかありません。 – Thomas

0

あなただけのSOCを作成していますサーバーに接続しないでください。 socket.create_connectionを使用する必要があります。 また、socket.errorとsocket.timeoutを一緒にまとめることは、デバッグ時に誤解を招く可能性があるため、お勧めできません。また、一般的なメッセージだけでなく、エラーを出力する必要があります。何が間違っているか把握するのに役立ちます。

+0

どういう意味で、どこに接続を作成するのですか? – 21days

+0

socket.socket()はソケットを作成するだけですが、接続しません。 socket.create_connection()はソケットを作成し、サーバに接続します。調べる。 –

+0

self.irc = socket.create_connection((self.server、self.port)) まだ問題が解決していません。 – 21days

0

iStartはどこにも電話しません。自分のIRCを正しく覚えていれば、データを送り返す前にあなたのニック情報を送信する必要があります。

+0

bot = Pibot() bot.iConnect() bot.iStart() bot.MainLoop() – 21days

+0

@ 21days:これは上記のコード例にはありません。 –

+0

はい、申し訳ありませんが、今は問題ですが、問題が発生しているコードはTraceback(最近の最後の呼び出し)です: ファイル "L:\ txtbot.py"、119行目、 bot。 MainLoop() MainLoopのファイル "L:\ txtbot.py"(64行目) self.irc.send(( "PONG%s \ r \ n")%(self.recv.split()[1]) )#可能なオーバーフローの問題 AttributeError:ピボットインスタンスに属性 'recv'がありません – 21days

関連する問題