gpt4 book ai didi

python - 使用 pexpect 从 virtualbox 监听端口

转载 作者:可可西里 更新时间:2023-11-01 02:53:02 25 4
gpt4 key购买 nike

我正在尝试在 python 中创建一个 tcplistener(必要时使用 pexpect)以在 Windows xp 主机上的 virtualbox 中监听来自 Ubuntu 的 tcp 连接。如果你们中的任何一位能指出正确的方向,我将不胜感激。谢谢。

P.S:我在该领域的经验有限,欢迎任何帮助。

最佳答案

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()

关于python - 使用 pexpect 从 virtualbox 监听端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3680567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com