我如何在 python 中一次检查两个到套接字的连接
How do i check for two connections to a sockets at once in python
我正在 运行ning 一个 python 脚本来侦听与我的计算机的传入连接。我正在监听两个端口 9999 和 9998。但是,当我使用 .accept() 检查与我的第一个端口的连接时,它会一直等待,直到我连接到 运行 下面的代码意味着我的另一个端口可以检查用于连接。
我希望能够同时检查连接并运行根据已连接的端口进行编码,这是我当前的代码。
import socket
import subprocess
import os
while 1 > 0:
# Set connection for C_shutdown script on port 9999
HOST = '192.168.1.46'
PORT = 9999
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(10)
print("Listening for Shutdown connection")
# Set connection for Light_on script on port 9998
HOST = '192.168.1.46'
PORT2 = 9998
light_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
light_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
light_socket.bind((HOST, PORT2))
light_socket.listen(10)
print("Listening for Light Connection")
l = light_socket.accept()
print("Light script connected")
s = server_socket.accept()
print("Shutdown Script Connected")
每个服务器都应该 运行 在其单独的线程中,如下所示:
# ... other imports ...
import threading
def server_job():
# ... Code for server_socket ...
print("Listening for server connection")
while True:
s = server_socket.accept()
# ... Anything you want to do with s ...
def light_job():
# ... Code for light_socket ...
print("Listening for Light Connection")
while True:
l = light_socket.accept()
# ... Anything you want to do with l ...
# Creates Thread instances that will run both server_job and light_job
t_server = threading.Thread(target=server_job)
t_light = threading.Thread(target=light_job)
# Begin their execution
t_server.start()
t_light.start()
# Wait for both threads to finish their execution
t_light.join()
t_server.join()
这里每个线程都有它的 while True 循环来接受多个连接,绑定和监听一个端口应该只发生一次。
其他选项是生成两个进程而不是两个线程,但这取决于您的用例,例如,如果您不需要在这些线程之间共享数据并且想要 GIL. In that case, might want to take a look at https://docs.python.org/3/library/multiprocessing.html[ 的解决方法=12=]
我正在 运行ning 一个 python 脚本来侦听与我的计算机的传入连接。我正在监听两个端口 9999 和 9998。但是,当我使用 .accept() 检查与我的第一个端口的连接时,它会一直等待,直到我连接到 运行 下面的代码意味着我的另一个端口可以检查用于连接。
我希望能够同时检查连接并运行根据已连接的端口进行编码,这是我当前的代码。
import socket
import subprocess
import os
while 1 > 0:
# Set connection for C_shutdown script on port 9999
HOST = '192.168.1.46'
PORT = 9999
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(10)
print("Listening for Shutdown connection")
# Set connection for Light_on script on port 9998
HOST = '192.168.1.46'
PORT2 = 9998
light_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
light_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
light_socket.bind((HOST, PORT2))
light_socket.listen(10)
print("Listening for Light Connection")
l = light_socket.accept()
print("Light script connected")
s = server_socket.accept()
print("Shutdown Script Connected")
每个服务器都应该 运行 在其单独的线程中,如下所示:
# ... other imports ...
import threading
def server_job():
# ... Code for server_socket ...
print("Listening for server connection")
while True:
s = server_socket.accept()
# ... Anything you want to do with s ...
def light_job():
# ... Code for light_socket ...
print("Listening for Light Connection")
while True:
l = light_socket.accept()
# ... Anything you want to do with l ...
# Creates Thread instances that will run both server_job and light_job
t_server = threading.Thread(target=server_job)
t_light = threading.Thread(target=light_job)
# Begin their execution
t_server.start()
t_light.start()
# Wait for both threads to finish their execution
t_light.join()
t_server.join()
这里每个线程都有它的 while True 循环来接受多个连接,绑定和监听一个端口应该只发生一次。
其他选项是生成两个进程而不是两个线程,但这取决于您的用例,例如,如果您不需要在这些线程之间共享数据并且想要 GIL. In that case, might want to take a look at https://docs.python.org/3/library/multiprocessing.html[ 的解决方法=12=]