简体   繁体   English

python听2端口相同的文件

[英]python listen 2 port same file

I would like to listen on 2 different UDP port with the same server. 我想用同一台服务器监听2个不同的UDP端口。 I use SocketServer lib for my server, and basicly it looks like that; 我为我的服务器使用SocketServer lib,基本上它看起来像那样;

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

I would like to listen on 7878 and 7879 with the same server and same file. 我想听7878和7879使用相同的服务器和相同的文件。 Is that possible? 那可能吗? If yes how? 如果有,怎么样?

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: 它创建了一个服务器来监听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

Note: this was taken from the docs of the SocketServer module, and modified with threads. 注意:这是从SocketServer模块的文档中获取的,并使用线程进行修改。

Nope. 不。 Consider using Twisted . 考虑使用Twisted

No need to use threads for something like this. 不需要使用线程来做这样的事情。 Consider http://code.google.com/p/pyev/ 考虑http://code.google.com/p/pyev/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在一个端口上侦听并同时从另一个端口广播? 蟒蛇 - How to listen on a port and broadcast from another at the same time? Python 侦听python中已用于UDP数据包的端口吗? - Listen to a port already in use for UDP packets in python? 始终侦听特定端口号的Python应用程序 - Python Application that Always Listen to Specific Port Number Python gRPC服务器未在指定端口上侦听 - Python gRPC server does not listen on specified port 如何在python + linux中并行监听端口 - How to listen port parallel in python + linux Flask允许多个服务器实例在同一端口上侦听 - Flask allows multiple server instances to listen on the same port 父子在同一端口上侦听时会发生什么? - What happens when parent-child listen on the same port? 如何编写可以同时监听端口的多线程kivy游戏(在rasp Pi上) - how to write a multithread kivy game(on rasp Pi) that can listen to a port at the same time python:如何同时监听鼠标点击和按键 - python: how to listen for mouse clicks and keypress at the same time 如何在python中同时监听命名管道和套接字 - How to listen named pipe and socket at the same time in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM