运行 python asyncio socks 代理服务器在线程中时出错
error when running python asyncio socks proxy server in a thread
我正在使用 python3 asyncio_socks_server 库来 运行 socks 代理,我的程序应该 运行 线程中的 socks 代理和 HTTP 服务器然后另一个线程继续执行。
这是我的 socks_proxy.py:
import socket
from asyncio_socks_server.__version__ import __version__
from asyncio_socks_server.app import SocksServer
class SocksProxy:
def __init__(self, local_ip, port):
args = []
config_args = {
"LISTEN_HOST": local_ip,
"LISTEN_PORT": port,
"AUTH_METHOD": 0,
"ACCESS_LOG": 0,
"DEBUG": 1,
"STRICT": 0,
}
app = SocksServer(
config=None,
env_prefix="AIOSS_",
**{k: v for k, v in config_args.items() if v is not None},
)
app.run()
def startSocksProxy(local_ip, port):
s = SocksProxy(local_ip, port)
main.py:
import threading
from modules.util.socks_proxy import startSocksProxy
from modules.logger.logger import Logger
if "__main__" == __name__:
logger = Logger()
remoteLog = threading.Thread(target=startLoggerServer)
remoteLog.start()
socksProxy = threading.Thread(target=startSocksProxy, args=(LOCAL_IP, 7000))
socksProxy.start()
HTTP 服务器启动正常,但是,我收到以下错误:
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "j:\WormScanner\modules\util\socks_proxy.py", line 35, in startSocksProxy
s = SocksProxy(local_ip, port)
File "j:\WormScanner\modules\util\socks_proxy.py", line 26, in __init__
app = SocksServer(
File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\asyncio_socks_server\app.py", line 23, in __init__
self.loop = asyncio.get_event_loop()
File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\asyncio\events.py", line 639, in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-2'.
并且 socks 代理没有启动。
对于曾经遇到过这个问题的任何人,当我使用多处理时它工作正常:
if "__main__" == __name__:
multiprocessing.freeze_support()
remoteLog = multiprocessing.Process(target=startLoggerServer)
remoteLog.start()
socksProxy = multiprocessing.Process(target=startSocksProxy, args=(LOCAL_IP, 7000))
socksProxy.start()
我正在使用 python3 asyncio_socks_server 库来 运行 socks 代理,我的程序应该 运行 线程中的 socks 代理和 HTTP 服务器然后另一个线程继续执行。 这是我的 socks_proxy.py:
import socket
from asyncio_socks_server.__version__ import __version__
from asyncio_socks_server.app import SocksServer
class SocksProxy:
def __init__(self, local_ip, port):
args = []
config_args = {
"LISTEN_HOST": local_ip,
"LISTEN_PORT": port,
"AUTH_METHOD": 0,
"ACCESS_LOG": 0,
"DEBUG": 1,
"STRICT": 0,
}
app = SocksServer(
config=None,
env_prefix="AIOSS_",
**{k: v for k, v in config_args.items() if v is not None},
)
app.run()
def startSocksProxy(local_ip, port):
s = SocksProxy(local_ip, port)
main.py:
import threading
from modules.util.socks_proxy import startSocksProxy
from modules.logger.logger import Logger
if "__main__" == __name__:
logger = Logger()
remoteLog = threading.Thread(target=startLoggerServer)
remoteLog.start()
socksProxy = threading.Thread(target=startSocksProxy, args=(LOCAL_IP, 7000))
socksProxy.start()
HTTP 服务器启动正常,但是,我收到以下错误:
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "j:\WormScanner\modules\util\socks_proxy.py", line 35, in startSocksProxy
s = SocksProxy(local_ip, port)
File "j:\WormScanner\modules\util\socks_proxy.py", line 26, in __init__
app = SocksServer(
File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\asyncio_socks_server\app.py", line 23, in __init__
self.loop = asyncio.get_event_loop()
File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\asyncio\events.py", line 639, in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-2'.
并且 socks 代理没有启动。
对于曾经遇到过这个问题的任何人,当我使用多处理时它工作正常:
if "__main__" == __name__:
multiprocessing.freeze_support()
remoteLog = multiprocessing.Process(target=startLoggerServer)
remoteLog.start()
socksProxy = multiprocessing.Process(target=startSocksProxy, args=(LOCAL_IP, 7000))
socksProxy.start()