简体   繁体   中英

How to make asynchronous HTTP POST requests

I have this program which runs in a loop with pythoncom.PumpMessages() . While this program runs, it takes input and stores it iternally. When the input reaches a certain lenght, I'd like to send a HTTP POST request asynchronously to a database I have in the cloud so the program doesn't stop taking input while the request is sent. I do not need the request from the server, although it would be nice.

Is this possible? I'm having a hard time figuring this out. Right now it does it synchronously.

Thanks!

This can be done if you use python requests library to send post requests. It has been answered here. Asynchronous Requests with Python requests The example is for "GET" request but you can easily do post request as well.

JavaScript works on any browser without added libraries.

This is great for loading parts of a page without stalling the UI, but use another method (eg server or NodeJS) if sending many requests (eg >100).

<p id="demo">Customer info will be listed here...</p>

<script>
function showCustomer(str) {
  var xmlhttp;
  if (str == "") {
    document.getElementById("demo").innerHTML = "";
    return;
  }
  xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML = this.responseText;
    }
  };
  xmlhttp.open("GET", "yourFile.php?queryVariable="+str, true);
  xmlhttp.send();
}
</script>

Source: http://www.w3schools.com/xml/tryit.asp?filename=try_dom_xmlhttprequest_database

GET: http://www.w3schools.com/xml/tryit.asp?filename=try_dom_xmlhttprequest_first

More here: http://www.w3schools.com/xml/dom_http.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM