2010-09-09 59 views
1

我想在python中使用pexpect创建tcplistener(如果有必要)在Windows XP主机上侦听来自Ubuntu的虚拟机中的tcp连接。我真的很感激,如果你们中的一个能够指引我正确的方向。谢谢。使用pexpect在虚拟机上侦听端口

P.S:我在该地区的经验有限,任何帮助将受到欢迎。

+0

这将有助于如果你能回答以下几个问题:哪里是你的代码运行? Windows主机或Ubuntu客户?你为什么认为有必要?简单地说,你试图完成什么? – Rakis 2010-09-09 21:09:39

回答

1

Python已经在标准库中提供了一个简单的套接字服务器,该套接字服务器名称为SocketServer。如果你想要的是一个基本的倾听者,看看这个example straight from the documentation

import SocketServer 

class MyTCPHandler(SocketServer.BaseRequestHandler): 
    """ 
    The RequestHandler class for our server. 

    It is instantiated once per connection to the server, and must 
    override the handle() method to implement communication to the 
    client. 
    """ 

    def handle(self): 
     # self.request is the TCP socket connected to the client 
     self.data = self.request.recv(1024).strip() 
     print "%s wrote:" % self.client_address[0] 
     print self.data 
     # just send back the same data, but upper-cased 
     self.request.send(self.data.upper()) 

if __name__ == "__main__": 
    HOST, PORT = "localhost", 9999 

    # Create the server, binding to localhost on port 9999 
    server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler) 

    # Activate the server; this will keep running until you 
    # interrupt the program with Ctrl-C 
    server.serve_forever() 
+0

非常感谢。 – 2010-09-11 21:21:06

+0

非常欢迎。请考虑接受我的答案作为正确答案! :) – jathanism 2010-09-14 14:00:13