我目前正在用Python寫一個telnet服務器。這是一個內容服務器。人們將通過telnet連接到服務器,並顯示純文本內容。多個同時網絡連接 - Telnet服務器,Python
我的問題是,服務器顯然需要支持多個同時連接。我現在的實現現在只支持一個。
這是我開始與基本的,證據的概念服務器(而該計劃已隨着時間的推移很大變化,基本的telnet框架還沒有):
import socket, os
class Server:
def __init__(self):
self.host, self.port = 'localhost', 50000
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.bind((self.host, self.port))
def send(self, msg):
if type(msg) == str: self.conn.send(msg + end)
elif type(msg) == list or tuple: self.conn.send('\n'.join(msg) + end)
def recv(self):
self.conn.recv(4096).strip()
def exit(self):
self.send('Disconnecting you...'); self.conn.close(); self.run()
# closing a connection, opening a new one
# main runtime
def run(self):
self.socket.listen(1)
self.conn, self.addr = self.socket.accept()
# there would be more activity here
# i.e.: sending things to the connection we just made
S = Server()
S.run()
感謝您的幫助。
+1:扭曲是要走的路。實現一個簡單的telnet服務器應該很容易。 – nosklo 2009-04-22 11:11:52