我需要創建一個簡單的服務器來偵聽TCP連接。 如果它接收到文本on<EOF>
或off<EOF>
,則它將發回(回顯)回success
。接收部分正在工作,但現在我需要它發回success
。Python,如何通過TCP發送數據
代碼:
# import threading
import SocketServer
class TCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
self.msg = self.request.recv(1024).strip()
if self.msg == "on<EOF>":
print "Turning On..."
#ECHO "SUCCESS<EOF>" <----- I need the server to echo back "success"
if self.msg == "off<EOF>":
print "Turning Off..."
#ECHO "SUCCESS<EOF>" <----- I need the server to echo back "success"
if __name__ == "__main__":
host, port = '192.168.1.100', 1100
# Create server, bind to local host and port
server = SocketServer.TCPServer((host,port),TCPHandler)
print "server is starting on ", host, port
# start server
server.serve_forever()
'>'是否標記應用程序消息的結尾?如果是這樣,閱讀郵件的代碼在哪裏?如果不是,消息如何分隔? –