2017-07-29 4 views
0

APIサーバーとしてフラスコを使用します。 私はPythonでそれをテストします。それは大丈夫です!フラスコは郵便配達員やウェブページから投稿データを取得することはできませんが、Pythonリクエストで動作します

class ModelLoader(MethodView): 
    def __init__(self): 
     pass 

    def post(self): 
     content = request.get_json() 
     request 
     X_input = content['X_input'] 
     X_in = str(X_input) 
     pred_val,posib = predictor.predict(X_input=X_in) 
     pred_val = pred_val.tolist() 
     posib = str(round(posib,4)*100) 
     #posib = posib.tolist() 
     return jsonify({'pred_val': pred_val,'confident':posib}) 

    def initialize_models(json_path, weights_path, normalized_x, normalized_y): 
     global predictor 
     predictor = Predictor(json_path, weights_path, normalized_x, normalized_y) 
     predictor.compile_model(loss='categorical_crossentropy', optimizer='adam') 


    def run(host='0.0.0.0', port=19865): 
     """Run a WSGI server using gevent.""" 
     app.add_url_rule('/predict', 
     view_func=ModelLoader.as_view('predict')) 
     print('running server http://{0}'.format(host + ':' + str(port))) 
     WSGIServer((host, port), app).serve_forever() 

テストコード

import requests 
def get_predictions(X_input): 
    """Get predictions from a rest backend for your input.""" 
    print("Requesting prediction for XOR with {0}".format(X_input)) 
    r = requests.post("http://yansheng.wang:19865/predict", json={'input': X_input}) 
print(r.status_code, r.reason) 
resp = r.json() 
prediction = resp['pred_val'] 
confident = resp['confident'] 
print("XOR of input: {0} is {1},on confident of {2} ".format(X_input, prediction,confident)) 

if __name__ == '__main__': 

    X_inputs = ["885863343916543724"] 

    for x_input in X_inputs: 
     get_predictions(x_input) 

私はそれを実行したとき、私は郵便配達やWebページからポストデータを取得することはできません!

サーバーのコンソール出力の違いは、あなたがJSONボディにX_input値を渡しているテストコードでコードをテストしているとき

45.76.182.34 - - [2017-07-29 12:11:55] "POST /predict HTTP/1.1" 200 162 0.044396 
45.76.182.34 - - [2017-07-29 12:12:03] "POST /predict?input=3423432432424233 HTTP/1.1" 400 303 0.001046 

答えて

0

?:間違っているものを、以下のようなものです。

# part of request 
json={'input': X_input} 

# I would suggest make the key consistent 

# inside post menthod 
content = request.get_json() 
request 
X_input = content['X_input'] 
X_in = str(X_input) 

しかし、Web/Postmanから送信する場合は、url paramsで送信しています。

"POST /predict?input=3423432432424233 HTTP/1.1" 400 

これは失敗です。 json本体の郵便配達員からこのデータを送信します。そしてそれはうまくいくはずです。

例:

postman post call example with json body

+0

あなたは歓迎@rockkingです:) –

関連する問題