简体   繁体   English

如何发出异步HTTP POST请求

[英]How to make asynchronous HTTP POST requests

I have this program which runs in a loop with pythoncom.PumpMessages() . 我有这个程序与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. 当输入达到一定长度时,我想将HTTP POST请求异步发送到我在云中拥有的数据库,这样程序在发送请求时不会停止接收输入。 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. 如果您使用python请求库发送帖子请求,则可以完成此操作。 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. 带有Python请求异步请求该示例用于“ GET”请求,但您也可以轻松地执行发布请求。

JavaScript works on any browser without added libraries. JavaScript可以在任何不添加库的浏览器上使用。

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). 这对于在不停止UI的情况下加载页面的某些部分非常有用,但是如果发送许多请求(例如,> 100),则使用另一种方法(例如,服务器或NodeJS)。

<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 资料来源: 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 GET: http : //www.w3schools.com/xml/tryit.asp? filename= try_dom_xmlhttprequest_first

More here: http://www.w3schools.com/xml/dom_http.asp 此处更多信息: http : //www.w3schools.com/xml/dom_http.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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