[英]Flask works locally, but not on a server?
我正在開發一個 Flask 應用程序,它目前接受請求並回答它們。 但是我在本地開發它並且它工作得很好,但是當嘗試在repl.it
上運行它時,它不起作用(請求引發“Winerror 10060”:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
)
這是為什么? (我知道, repl.it
使用的 IP 與我在本地使用的 IP 不同,但我已經切換了它們。)
客戶:
import json
import requests
conv = [{'id': 1, 'key': 'get_password'}]
payload = json.dumps(conv)
res = requests.post("http://127.0.0.1:5000", json=payload).json()
print(res)
服務器:
from flask import Flask, request
import json
app = Flask(__name__)
@app.route('/', methods = ['POST'])
def home():
jsondata = request.get_json()
data = json.loads(jsondata)
#stuff happens here that uses the data
print(data)
result = {'response': True}
return json.dumps(result)
if __name__ == '__main__':
app.run(host = "0.0.0.0", port = 5000)
發生錯誤是因為您在地址127.0.0.1
上發出請求,以防 repl.it 不使用該地址。
對 repl.it 提供的 url 發出請求。
網址將是:
https://your-app-name.your-replit-username.repl.co/
你的代碼是
import json
import requests
conv = [{'id': 1, 'key': 'get_password'}]
payload = json.dumps(conv)
res = requests.post("https://moneyserver.jgroeger.repl.co/", json=payload).json()
print(res)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.