我一直在使用python中的telnet并且遇到了一些设计良好的代码,但我遇到的问题是我希望能够通过提供用户名和密码来查看谁登录。我已经完成了对telnet和python模块的研究,但是对于我的生活,我无法弄明白。如果有人可以展示一个很棒的例子。
这是我的代码:
import socket, threading
HOST = '192.168.177.141'
PORT = 51234
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(4)
clients = [] #list of clients connected
lock = threading.Lock()
class chatServer(threading.Thread):
def __init__(self, (socket,address)):
threading.Thread.__init__(self)
self.socket = socket
self.address= address
def run(self):
lock.acquire()
clients.append(self)
lock.release()
print '%s:%s connected.' % self.address
while True:
data = self.socket.recv(1024)
if not data:
break
for c in clients:
c.socket.send(data)
self.socket.close()
print '%s:%s disconnected.' % self.address
lock.acquire()
clients.remove(self)
lock.release()
while True:
chatServer(s.accept()).start()