2017-01-16 4 views
0

私のpythonクライアントがフラスコサーバーにPOSTリクエストを送信していて、最初にサーバー上の変数を更新しますが、クライアントにループすると、決して更新されません。理由は何ですか?繰り返しますが、クライアント上のforループを削除して手動で実行すると、毎回サーバー上のPOST変数が更新されます。タイムスタンプが更新されていないことを出力から見ることができます...セッションを終了していない限り、なぜforループなしで更新されるのかという手掛かりはありませんか?前もって感謝します!FlaskアプリケーションがWHILEループでクライアントから更新されたPOST情報を収集しない

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

あなたは 'while 1:time.sleep(3)'を参照していますか? 'timestamp = strftime("%m-%d-%Y /%H:%M:%S ")'はwhileループの外側で計算されるため、更新できないようです。 – roganjosh

+1

はい、 'while'ループの各繰り返しで同じ値を持ちます。あなたがwhileループの中でそれを再計算しないと、それはループに入っていたどんな値でも永久に止まってしまいます。ダニエルの答えによると、これはフラスコとは関係ありません。これはPythonの標準です – roganjosh

答えて

1

は、これはフラスコとは何の関係もありません。

クライアントはループの外で定義して更新しないため、毎回同じタイムスタンプを送信します。 whileループ内でstrftime呼び出しを移動します。

+0

私は今世界で最も大きなダミーのように感じます! –

関連する問題