gpt4 book ai didi

Python 操作系统错误 : [Errno 98] Address already in use but no port is used

转载 作者:行者123 更新时间:2023-12-02 16:18:37 26 4
gpt4 key购买 nike

我的代码同时使用 flask(http,作为主线程)、ngrok 和 tcp 服务器(ngrok 和 tcp 服务器都在两个线程上运行)时遇到问题(错误消息见下文)

Traceback (most recent call last):
File "/home/monki/anaconda3/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/home/monki/anaconda3/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/home/monki/VSCode/Python/Webhook/app.py", line 49, in launchServer
s.bind((hostname, TCP_PORT))
OSError: [Errno 98] Address already in use

我检查了端口使用

ps -fA | grep python

发现没有端口被占用

root         910       1  0 15:02 ?        00:00:00 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
root 1036 1 0 15:02 ? 00:00:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
x 6316 5499 0 15:55 pts/0 00:00:00 grep --color=auto python

这是我的代码

from flask import Flask, request, Response
import json
import socket
import threading
from pyngrok import ngrok

from queue import Queue

#tcp server
TCP_IP = ''
TCP_PORT = 13370
BUFFER_SIZE = 20

# http server
HOST_URL = 'http://xxx.ngrok.io'


def start_ngrok():
### sorry i have to blank out the account
ngrok.set_auth_token("xxxxxx")
url = ngrok.connect(5000, subdomain='xxx').public_url
print(' * Tunnel URL:', url)


def launchServer(out_q):
print("TCP thread started")
hostname = socket.gethostname()
local_ip = socket.gethostbyname(hostname)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

s.bind((hostname, TCP_PORT))
s.listen(1)

while True:
print('waiting for a connection')
conn, addr = s.accept()
try:
print(sys.stderr + 'client connected:' + addr)
while True:
out_q.put(data)
data = conn.recv(1024)

# print >>sys.stderr, 'received "%s"' % data
if data:
conn.sendall(data)
else:
break
finally:
conn.close()

app = Flask(__name__)

# route definition
@app.route('/<webhookAddr>', methods=['POST'])
def response(in_q, webhookAddr):

if webhookAddr == 'getGithub':
# data = json.loads(request.data)
print(request.json)

in_q.put(request.json)
return Response(status=200)

elif webhookAddr == 'getGitHub2':
print(request.json)
return Response(status=200)
else:
return Response(status=200)


if __name__ == '__main__':
q = Queue()

t1 = threading.Thread(target=launchServer, args =(q, ))
t1.daemon = True
t1.start()

t2 = threading.Thread(target=start_ngrok)
t2.daemon = False
t2.start()

app.run(debug=True)

然而,通过单独运行 launchServer 的代码部分,我没有得到这样的错误。所以我错误地使用了ngrok。需要你的帮助指出我的错误

问候

最佳答案

我这边也重现了这个问题。从调试 Flask 代码来看,在 Debug模式下似乎没有使用 SO_REUSEADDR,因此下一次尝试绑定(bind)同一地址(您的 tcp 服务器)将会失败。一种选择是关闭 Debug模式:

app.run(debug=False)

另一种选择是关闭重新加载器(如果您在服务器运行时修改应用程序,重新加载器非常有用):

app.run(debug=True, use_reloader=False)

关于Python 操作系统错误 : [Errno 98] Address already in use but no port is used,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66074765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com