python监听2个端口相同的文件

发布于 2024-08-18 10:45:46 字数 178 浏览 7 评论 0原文

我想用同一台服务器监听 2 个不同的 UDP 端口。 我的服务器使用 SocketServer lib,基本上它看起来像这样;

SocketServer.UDPServer(('', 7878),CLASSNAME)

我想用相同的服务器和相同的文件监听 7878 和 7879。 这可能吗?如果是的话怎么办?

I would like to listen on 2 different UDP port with the same server.
I use SocketServer lib for my server, and basicly it looks like that;

SocketServer.UDPServer(('', 7878),CLASSNAME)

I would like to listen on 7878 and 7879 with the same server and same file.
Is that possible? If yes how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

真心难拥有 2024-08-25 10:45:46

当然可以,使用线程。这是一个服务器:

import SocketServer
import threading


class MyUDPHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        data = self.request[0].strip()
        socket = self.request[1]
        print "%s wrote:" % self.client_address[0]
        print data
        socket.sendto(data.upper(), self.client_address)


def serve_thread(host, port):
    server = SocketServer.UDPServer((host, port), MyUDPHandler)
    server.serve_forever()


threading.Thread(target=serve_thread,args=('localhost', 9999)).start()
threading.Thread(target=serve_thread,args=('localhost', 12345)).start()

它创建一个服务器来监听 9999,另一个服务器监听 12345。
下面是一个可用于测试此功能的示例客户端:

import socket
import sys

HOST, PORT = "localhost", 12345
data = 'da bomb'

# SOCK_DGRAM is the socket type to use for UDP sockets
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# As you can see, there is no connect() call; UDP has no connections.
# Instead, data is directly sent to the recipient via sendto().
sock.sendto(data + "\n", (HOST, PORT))
received = sock.recv(1024)

print "Sent:     %s" % data
print "Received: %s" % received

注意:这取自 SocketServer 模块的文档,并使用线程进行了修改。

Sure you can, using threads. Here's a server:

import SocketServer
import threading


class MyUDPHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        data = self.request[0].strip()
        socket = self.request[1]
        print "%s wrote:" % self.client_address[0]
        print data
        socket.sendto(data.upper(), self.client_address)


def serve_thread(host, port):
    server = SocketServer.UDPServer((host, port), MyUDPHandler)
    server.serve_forever()


threading.Thread(target=serve_thread,args=('localhost', 9999)).start()
threading.Thread(target=serve_thread,args=('localhost', 12345)).start()

It creates a server to listen on 9999 and another to listen on 12345.
Here's a sample client you can use for testing this:

import socket
import sys

HOST, PORT = "localhost", 12345
data = 'da bomb'

# SOCK_DGRAM is the socket type to use for UDP sockets
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# As you can see, there is no connect() call; UDP has no connections.
# Instead, data is directly sent to the recipient via sendto().
sock.sendto(data + "\n", (HOST, PORT))
received = sock.recv(1024)

print "Sent:     %s" % data
print "Received: %s" % received

Note: this was taken from the docs of the SocketServer module, and modified with threads.

拔了角的鹿 2024-08-25 10:45:46

没有。考虑使用 Twisted

Nope. Consider using Twisted.

薄荷港 2024-08-25 10:45:46

不需要使用线程来做这样的事情。考虑http://code.google.com/p/pyev/

No need to use threads for something like this. Consider http://code.google.com/p/pyev/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文