0

I built a simple flask server in order to automate python scripts based on an HTTP POST Request coming in from an outside service. This service sends these requests in response to events that we have created based on data conditions. All the flask server needs to do is parse the requests, and take the numerical value stored in the request and use it to run the corresponding python script. At one point, this system was working consistently. Fast forward a few months, the server was no longer working when tried again. No changes were made to the code. According to wireshark, The computer hosting the flask server is receiving the request to the correct port, but the request is now timing out. The host system is failing to respond to the request. Any Idea what is going on here? The firewall has temporarily been turned off.

Alternatively, is there another better package to achieve this goal with?

from flask import Flask, request
import threading
import runpy

app = Flask(__name__)


@app.route('/', methods=['POST'])
def PostHandler():
    directory = {}
    with open(r"M:\redacted") as f:
        for line in f:
            (key,val) = line.split()
            directory[int(key)] = val
    print(directory)
    path = r"M:\redacted"
   

    
    content = request.json
    content = content['value']
    print(content)
    sel_script = int(content)
    
    print(directory[sel_script])
   
    runpy.run_path(path_name=path + directory[sel_script])

    return


app.run(host="10.244.xx.xx", port=8080, threaded=True)

This is the POST request I am sending via Postman. Which of the settings matter? I have the firewall turned off.

  • maybe you should use `print()` to see what you get in Flask. If code is the same then maybe server has some changes. Maybe server save some values in logs so you could get some inforamtion. – furas Dec 01 '21 at 16:53
  • Typically if the value had been received or acted on by flask, I would see some information about the request in the pycharm console. Now, I am not seeing this information. Flask is not responding to the request in any way. I have previously been able to receive GET requests that I generated via Postman, even when I was unable to see POST requests. My thinking is the problem may be linked to the method used. I do not have enough understanding of the HTTP handshake to see beneath the black box of the protocol. – Steve Vitale Dec 01 '21 at 17:47
  • I don't know how you send request to `flask` but if flask uses ` methods=['POST']` then it can receive only `POST` but not `GET`. Of course it may run on different port or IP then you use in requests. Maybe use standard `host='0.0.0.0'` to receive requests on all network cards in server. Code may also raise some error and it can't send response. If you run it using `Apache` or `Nginx` then they should save some messages in logs. – furas Dec 01 '21 at 18:01
  • maybe you should also use `logging` to save information in file - or run with `debug=True` and test in web browsers to see error messages. – furas Dec 01 '21 at 18:05
  • I have it now set to both GET and POST methods, nothing has changed. I also changed to 0.0.0.0, no changes. When I access the flask server via the web page, it registers that GET request in the pycharm console. When I send a GET request from Postman from another machine, I dont get the same message. I have confirmed that the port on the IP is receiving the requests via wireshark but again it does not get a response from Flask. – Steve Vitale Dec 01 '21 at 18:07
  • maybe you send to different computers, We can't test it, we don't have logs, we don't have error message - we can't help you. – furas Dec 01 '21 at 18:20
  • I am not understanding how you are suggesting I collect these logs. I did run with debug true. The problem doesnt seem to be in the code. When I access the debug function, I can read the error on the web server. This is because the code I am writing will not parse a GET request without a value stored in the body. However, I am seeing that this request is being recognized by flask. However, when I try to send any type of request from a different computer, I do not even get a response even in error. – Steve Vitale Dec 01 '21 at 19:00
  • if you can read erro on web server then why you didn't show FULL error in question. – furas Dec 01 '21 at 19:27
  • if you send from different computer then show how you do it - maybe you do it in wrong way. Maybe you send to wrong IP, or to wrong PORT. Simply: we can't read in your mind - you have to show all details in questions, And without details like your comman, error messages, logs we have no clue what can make problem and we can't help you. – furas Dec 01 '21 at 19:28
  • The "error" you are referring to is not the problem I am experiencing. The error is expected, and is an example that the flask code itself seems to be working correctly. This isnt an issue with port or IP, That is why I relayed the information about the wireshark - I have confirmed that the HTTP request is being sent to the correct place and is being received by the target computer. The crux of the issue is that I cant process any request sent by an outside computer, despite receiving the request. Is this some type of permissions or authentication issue? – Steve Vitale Dec 01 '21 at 19:49
  • You are doing ```content = request.json``` which means your Flask Server should be receiving JSON. Confirm you are sending JSON and the headers are correctly set on the sender ```Content-Type: application/json``` – NoCommandLine Dec 01 '21 at 22:20
  • That's a good catch! I was not specifying the content type in the header. Unfortunately it had been historically working without the header in there, and changing it now had no effect. – Steve Vitale Dec 02 '21 at 14:05
  • [This](https://stackoverflow.com/questions/70186964/flask-server-not-responding-to-http-post-request) page suggest some things that you have already tried but gives some additional suggestions. – Mike O'Connor Dec 02 '21 at 21:25
  • This is a link to my own post? – Steve Vitale Dec 03 '21 at 13:20
  • Silly me! [Here](https://stackoverflow.com/questions/7023052/configure-flask-dev-server-to-be-visible-across-the-network) is the link that I was supposed to have sent. – Mike O'Connor Dec 03 '21 at 20:37

1 Answers1

0

What the error in postman? I think these are the possibilities:

  1. if the error is immediately after you send request, maybe because you not return as flask can handle. try return with jsonify('ok'), 200
  2. if the error not appear immediately(timeout), maybe because the network, you need to setup inbound rules for network/security group on your server and open port 8080
Khaerul Umam
  • 95
  • 2
  • 7