2017-02-17 5 views
0

현재 로컬 네트워크의 트 위치에서 다른 컴퓨터로 메시지를 전달하는 서버 프레임 워크를 개발하려고합니다. 나는 서버라는 클래스를 가지고 있으며, 아래에는 내가 겪고있는 문제를 보여주는 기본적인 예제가있다. 문제는 twitch_socket이 두 번 만들어지고 주소/포트에 바인딩되어 있다는 것입니다. 내 기대 결과는 소켓이 Server 클래스의 자식 프로세스간에 공유된다는 것입니다. 프로세스가 소켓을 공유 할 수 있도록 클래스를 수정하거나 완전히 제거 할 수 있습니까? 콘솔에서클래스 내에서 생성되는 프로세스로 인해 '각 주소 오류의 소켓 사용량이 하나만 있습니다'를 어떻게 수정할 수 있습니까?

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 

답변

1

socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 추가 이전 실행이 TIME_WAIT 상태에서 소켓을 떠난, 그리고 SO_REUSEADDR 플래그가 알려줍니다 즉시 reused.the 할 수 없기 때문입니다 커널은 로컬 타임 아웃이 만기되기를 기다리지 않고 TIME_WAIT 상태의 로컬 소켓을 재사용한다.