我有一个问题,我的python客户端正在向烧瓶服务器发送POST请求,并且它最初将更新服务器上的变量,但如果我将FOR循环引入客户端,它将永远不会更新。任何想法为什么?同样,如果我在客户端上删除for循环并手动运行它,它将每次更新服务器上的POST变量。您可以从输出中看到时间戳没有更新...我不知道为什么它会在没有for循环的情况下更新,除非它没有关闭会话?提前谢谢!
app.py
from flask import Flask, render_template, request
import os, csv
import sqlite3
from sqlalchemy.orm import sessionmaker
from tabledef import *
from tabledef2 import *
app = Flask(__name__)
@app.route('/', methods=['POST', 'GET'])
def home():
conn = sqlite3.connect("client.db")
c = conn.cursor()
result = c.execute('SELECT * from clientdb')
host_array = []
for i in result:
#print str(i).split("'")[1]
host_array.append(str(i))
#host_array.append(str(i).split("'")[1])
return render_template('home1.html', message=host_array)
@app.route('/check', methods=['POST', 'GET'])
def check():
try:
host = request.form['hostname']
ip = request.form['ip']
ipex = request.form['ipex']
timestamp = request.form['timestamp']
code = request.form['code']
print host, ip, ipex, timestamp, code
return render_template('home1.html')
except IOError or ValueError or IOError.errno as e:
print "There was an error!"
return render_template('home1.html', message=e)
if __name__ == "__main__":
app.secret_key = os.urandom(12)
app.run(host='x.x.x.x', port=7000)
client.py
import socket, requests
from time import strftime
import time
import urllib2
timestamp = strftime("%m-%d-%Y / %H:%M:%S")
hostname = socket.gethostname()
ip = socket.gethostbyname(hostname)
ipex = requests.get('https://api.ipify.org').text
while 1:
time.sleep(3)
try:
URL = "http://10.1.1.33:7000/check"
payload = {
'hostname': "Server 1",
'ip': ip,
'ipex': ipex,
'timestamp': timestamp,
'code': 10
}
session = requests.session()
r = requests.post(URL, data=payload)
#print r.cookies
except socket.error:
print ("Connection was refused, trying to reconnect...")
输出
* Running on http://10.1.1.33:7000/ (Press CTRL+C to quit)
Server 1 10.1.1.33 68.115.121.98 01-16-2017 / 12:14:08 10
10.1.1.33 - - [16/Jan/2017 12:14:11] "POST /check HTTP/1.1" 200 -
10.1.1.33 - - [16/Jan/2017 12:14:14] "POST /check HTTP/1.1" 200 -
Server 1 10.1.1.33 68.115.121.98 01-16-2017 / 12:14:08 10
10.1.1.33 - - [16/Jan/2017 12:14:17] "POST /check HTTP/1.1" 200 -
Server 1 10.1.1.33 68.115.121.98 01-16-2017 / 12:14:08 10
10.1.1.33 - - [16/Jan/2017 12:14:20] "POST /check HTTP/1.1" 200 -
Server 1 10.1.1.33 68.115.121.98 01-16-2017 / 12:14:08 10
答案 0 :(得分:1)
这与Flask无关。
您的客户端每次都会发送相同的时间戳,因为您在循环外定义它并且永远不会更新它。在while循环中移动strftime
调用。