所以我有一个 flask 服务器和客户端。我想将数据从客户端发送到服务器,并将时间戳作为消息的一部分。这是我的代码:
import os
import requests
import glob
import time
import base64
url = 'http://0.0.0.0:5000/'
path = './10_mb'
folders = [f for f in glob.glob(path + '**/*', recursive = True)]
send = round(time.time() * 1000)
def load_data():
for folder in folders:
with open(folder, 'rb') as imageFile:
image_s = {'file_image':open(folder, 'rb')}
return image_s
def send_data():
r = requests.post(url, files = load_data(), headers = {'ts': str(send)})
if __name__ == "__main__":
send_data()
这是服务器
from flask import Flask
from flask import request
import time
app = Flask(__name__)
@app.route("/", methods = ['GET', 'POST'])
def home():
timeout = time.time() + 5
while True:
test = 0
if test ==5 or time.time() > timeout:
break
return 'Hello', 200
app.run(host='0.0.0.0', threaded = True, debug = True)
当我运行客户端时,从服务器端它不会打印来自客户端的时间戳。 我的问题是,如何从客户端获取时间戳?抱歉,如果这是一个愚蠢的问题,我完全是个菜鸟。提前致谢
最佳答案
Flask request中有很多attr 。其中,headers
返回您的 POST 的 header 。因此添加 print(request.headers)
就可以了。
# in server side...
@app.route("/", methods = ['GET', 'POST'])
def home():
timeout = time.time() + 5
while True:
test = 0
if test ==5 or time.time() > timeout:
break
print(request.headers)
return 'Hello', 200
app.run(host='0.0.0.0', threaded = True, debug = True)
# Host: 0.0.0.0:5000
# User-Agent: python-requests/2.22.0
# Accept-Encoding: gzip, deflate
# Accept: */*
# Connection: keep-alive
# Ts: 1577866093523
# Content-Length: 0
关于python - 从 flask 中的客户处获取时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59550610/