2017-09-19 8 views
1

私たちの小さなボットを実行すると、無限ループに陥り、終了することができません。それがないすべては、常に新しいボットを探しているすなわち:Pythonエコー出力は絶えず検索しますが終了しません

import socket 
import threading 

from lib.comms import StealthConn 
from lib.files import p2p_download_file 

# Keep track of where our server is 
# This is primarily so we don't try to talk to ourselves 
server_port = 1337 

def find_bot(): 
    print("Finding another bot...") 
    port = 1337 
    conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    while 1: 
     if port == server_port: 
      # Don't connect to yourself, silly bot! 
      port += 1 
     else: 
      try: 
       print("Found bot on port %d" % port) 
       conn.connect(("localhost", port)) 
       sconn = StealthConn(conn, client=True) 
       return sconn 
      except socket.error: 
       print("No bot was listening on port %d" % port) 
       port += 1 

def echo_server(sconn): 
    while 1: 
     data = sconn.recv() 
     print("ECHOING>", data) 
     sconn.send(data) 
     if data == b'X' or data == b'exit' or data == b'quit': 
      print("Closing connection...") 
      sconn.close() 
      return 

def accept_connection(conn): 
    try: 
     sconn = StealthConn(conn, server=True) 
     # The sender is either going to chat to us or send a file 
     cmd = sconn.recv() 
     if cmd == b'ECHO': 
      echo_server(sconn) 
     elif cmd == b'FILE': 
      p2p_download_file(sconn) 
    except socket.error: 
     print("Connection closed unexpectedly") 

def bot_server(): 
    global server_port 
    # Every bot is both client & server, so needs to listen for 
    # connections. This is to allow for peer to peer traffic. 
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    # Real worms use shifting ports but for simplicity, we won't. 
    # We'll also assume you may run another bot on your computer 
    # so if something else is using 1337, we'll keep going up. 
    while True: 
     try: 
      s.bind(("localhost", server_port)) 
      print("Listening on port %d" % server_port) 
      break 
     except socket.error: 
      # Someone is already using that port -- let's go up one 
      print("Port %d not available" % server_port) 
      server_port += 1 
    s.listen(5) 

    while 1: 
     print("Waiting for connection...") 
     conn, address = s.accept() 
     print("Accepted a connection from %s..." % (address,)) 
     # Start a new thread per connection 
     # We don't need to specify it's a daemon thread as daemon status is inherited 
     threading.Thread(target=accept_connection, args=(conn,)).start() 

以下

Waiting for connection... 
Enter command: p2p echo 
Finding another bot... 
Found bot on port 1338 
No bot was listening on port 1338 
Found bot on port 1339 
No bot was listening on port 1339 
Found bot on port 1340 
No bot was listening on port 1340 
Found bot on port 1341 
No bot was listening on port 1341 
Found bot on port 1342 

コードの周りを見て、それのような何かを見つけるように見えることはできません。

答えて

0

あなたがボットに接続しようとする前に、「発見ボット」の部分をプリントアウトあなたのtry-除く

 try: 
      print("Found bot on port %d" % port) 
      conn.connect(("localhost", port)) 
      sconn = StealthConn(conn, client=True) 
      return sconn 
     except socket.error: 
      print("No bot was listening on port %d" % port) 
      port += 1 

で例外を取得しています。 print("Found bot on port %d" % port)return sconnの間の例外があります。そのリターンステートメント、つまり無限ループを打つことはありません。 Whileは常にtrueです。

ボットを見つけたらsconnが値を返すと仮定すると、物事を並べ替えるだけです。

 try: 
      conn.connect(("localhost", port)) 
      sconn = StealthConn(conn, client=True) 
      if sconn: # Check for a return value that makes sense whatever 
         # StealthConn is 
       print("Found bot on port %d" % port) 
       return sconn 
     except socket.error: 
      print("No bot was listening on port %d" % port) 
      port += 1 
関連する問題