Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何更正由类中生成的进程导致的“每个地址仅使用一个套接字错误”?_Python_Python 3.x_Sockets_Multiprocessing_Twitch - Fatal编程技术网

Python 如何更正由类中生成的进程导致的“每个地址仅使用一个套接字错误”?

Python 如何更正由类中生成的进程导致的“每个地址仅使用一个套接字错误”?,python,python-3.x,sockets,multiprocessing,twitch,Python,Python 3.x,Sockets,Multiprocessing,Twitch,目前,我正在尝试开发一个服务器框架,将消息从twitch传递到本地网络上的其他机器。我有一个名为server的类,下面是一个基本示例,演示了我遇到的问题。问题是twitch_套接字被创建了两次并绑定到地址/端口。我的预期结果是,套接字将在服务器类的子进程之间共享。如何修改类,甚至完全删除它,使进程能够在它们之间共享套接字 import multiprocessing import socket import re from BotPass import PASSWORD def send_mes

目前,我正在尝试开发一个服务器框架,将消息从twitch传递到本地网络上的其他机器。我有一个名为server的类,下面是一个基本示例,演示了我遇到的问题。问题是twitch_套接字被创建了两次并绑定到地址/端口。我的预期结果是,套接字将在服务器类的子进程之间共享。如何修改类,甚至完全删除它,使进程能够在它们之间共享套接字

import multiprocessing
import socket
import re
from BotPass import PASSWORD
def send_message(socketobj, message):
    'Sends a str as bytes through socket'
     message = message.encode()
     socketobj.sendall(message)
def recv_message(socketobj):
    'Receives a str as bytes though socket'
    return socketobj.recv(2048).decode()
class Server:
    'Handles receiving messages from twitch and directs messages from clients'
    twitch_socket = socket.socket()
    twitch_socket.connect(('irc.chat.twitch.tv', 6667))
    send_message(twitch_socket, 'PASS %s\r\n' % (PASSWORD))
    send_message(twitch_socket, 'NICK %s\r\n' % ('squid_coin_bot'))
    send_message(twitch_socket, 'JOIN #jtv\r\n')
    send_message(twitch_socket, 'CAP REQ :twitch.tv/commands\r\n')
    server_socket = socket.socket()
    server_socket.bind(('', 9999))
    work_queue = multiprocessing.Queue()
    #Queue of messages from twitch
    worker_queue = multiprocessing.Queue()
    #Queue of free client socket objects
    result_queue = multiprocessing.Queue()
    #Queue of what to send back to twitch
    def start():
        accept_process = multiprocessing.Process(target=Server.accept_connections)
        # *This is most likely where the issue is occurring*
        accept_process.daemon = True
        accept_process.start()
    def accept_connections():
         ''
        Server.server_socket.listen(10)
        while 1:
            (clientsocket, clientaddr) = Server.server_socket.accept()
            # What I believe I am referencing here is the server socket which is inherent to the Server class
            if re.match(r'192\.168\.\d{1,3}\.\d{1,3}', clientaddr[0])\
             or clientaddr[0] == '127.0.0.1':
                Server.worker_queue.put(clientsocket)
            else:
                clientsocket.close()
Server.start()
input()
控制台中的输出:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Program Files\Python36\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "C:\Program Files\Python36\lib\multiprocessing\spawn.py", line 114, in _main
    prepare(preparation_data)
  File "C:\Program Files\Python36\lib\multiprocessing\spawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Program Files\Python36\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
    run_name="__mp_main__")
  File "C:\Program Files\Python36\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Program Files\Python36\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Program Files\Python36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\twitch-market\server.py", line 18, in <module>
    class Server:
  File "C:\twitch-market\server.py", line 27, in Server
    server_socket.bind(('', 9999))
OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted
添加此套接字。setsockoptsocket.SOL_套接字,socket.SO_REUSEADDR,1 这是因为上一次执行使套接字处于TIME\u WAIT状态,无法立即重用。SO\u REUSEADDR标志告诉内核在TIME\u WAIT状态下重用本地套接字,而不必等待其自然超时过期