[Python] Python telnet服务器 →→→→→进入此内容的聊天室

来自 , 2020-12-16, 写在 Python, 查看 126 次.
URL http://www.code666.cn/view/05c7ac94
  1. import threading  
  2.  
  3. class myThread(threading.Thread):
  4.     def __init__(self,conn,add):
  5.         threading.Thread.__init__(self)
  6.         self.inputstr = ''
  7.     self.connection=conn
  8.     self.address=add
  9.     def run(self):
  10.     ii=0
  11.         while True:
  12.         self.connection.settimeout(50)
  13.             buf = self.connection.recv(1024)
  14.         if  buf.rfind("\n") > -1 :  
  15.                 print "**-"+self.inputstr
  16.                 self.connection.close()
  17.                 break
  18.             else:  
  19.                 self.inputstr+=buf
  20.         if ii==0:
  21.             self.connection.send(buf)
  22.         ii+=1
  23.                 continue
  24.  
  25.            
  26.            
  27.  
  28.  
  29. if __name__ == '__main__':  
  30.    import socket  
  31.    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  32.    sock.bind(('192.168.101.103', 8014))  
  33.    sock.listen(5)
  34.    while True:  
  35.        try:
  36.            connection,address = sock.accept()
  37.            ithread=myThread(connection,address)
  38.            ithread.start()
  39.        except socket.timeout:  
  40.            print 'time out'
  41. #//python/1187

回复 "Python telnet服务器 "

这儿你可以回复上面这条便签

captcha