Search code examples
phppythonpython-requestslong-polling

Python long-polling pattern with requests


I'm doing the long-polling pattern with a PHP script and a MySQL database on a server and python script on the client. If the flag in the database is set to 0, the PHP page responds with the id of the tupel. If the request times out, a new request should be started. This is my code and I can't find my mistake:

gotID = False
ID = 0

while gotID == False:
    f = requests.get("http://example.de/ajax_backend.php")
    print("status: " + str(f.status_code))
    print("content: " + f.text)
    if int(f.status_code) == 200:
        gotID = True
        ID = f.text

If I run the code like this i get this output. First the flag was set to 1, then in the middle i changed the flag to 0:

console output python script

I think there is an error in the if statement, but I can't find it. Can you help me?


Solution

  • I now created a working code:

    gotID = None
    ID = 0
    
    while not gotID:
        f = requests.get("http://example.de/ajax_backend.php")
        print("status: " + str(f.status_code))
        print("content: " + f.text)
    
        try:
            ID = int(f.text)
            gotID = "samlpe content"
        except ValueError:
            gotID = None
    

    But i still don't know why my code above don't works.