Issue
I have a Windows client and a Linux server on the same local network. I have simple code on client:
import requests
requests.post(url=f"http://192.168.43.227:8080/get_info/", json={"login":"some_login", "password": "some_password"})
And here server code:
from flask import Flask, request, jsonify
import threading
app = Flask(__name__)
@app.route('/get_info', methods=['GET', 'POST'])
def authorization_func():
if request.method == 'POST':
data = jsonify(request.json)
print(data)
return data
if __name__ == '__main__':
threading.Thread(target=lambda: app.run(host='localhost', port=8080, debug=True, use_reloader=False)).start()
And when I start client on Windows, it writes "Failed to establish a new connection: [WinError 10061]". What's wrong?
I hope the server would response to client
Solution
You have asked your Flask app to bind to the localhost
address. That means it won't be accessible from anywhere but on the host on which it is running. You need to modify your app.run
statement to listen on all addresses:
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True, use_reloader=False)
I've removed your use of threading.Thread
here because it doesn't make any sense: this is the last line in your code; you're not attempting to perform other operations while the thread is running in the background.
Answered By - larsks
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.