javascript - 如何在 PHP 和 python 中使用 HTML5 SSE
问题描述
我想简单地检查一下我的 API 端点是否还活着,但我得到了Firefox can’t establish a connection to the server at myURL
错误,我在 94.0.1(64 位)Windows 版本上,这是基于此的支持。我正在 AWS-ec2 上尝试这个。这一切都基于这篇文章:https ://www.w3schools.com/html/html5_serversentevents.asp
这是我的HTML
:
<div id="searchDatabases">
</div>
<script type="text/javascript">
if(typeof(EventSource) !== "undefined") {
// Yes! Server-sent events support!
var source = new EventSource("pingServers.php");
source.onmessage = function(event) {
document.getElementById("searchDatabases").innerHTML = event.data + "<br>";
};
} else {
// Sorry! No server-sent events support..
console.info("Not supported browser!");
}
</script>
这是在pingServers.php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$output = shell_exec("python3 pingServers.py")
echo $output;
flush();
这是pingServers.py
#!/usr/bin/python
import requests
import json
import html
import sys
requestpost = requests.post('MyJSONEndpoint.php')
if requestpost.status_code == 200:
print('OK')
else:
print('not OK')
所以我的问题是我怎样才能正确地做到这一点?
解决方案
推荐阅读
- html - 在另一个组件Angular 7中绑定ngModel上的值
- facebook-graph-api - 如何以编程方式在 DNN 标头标签中包含元标签?
- python - 如何使用 PyLucene 从所有索引文档中检索特定字段?
- http - 如何将 http 服务调用返回的值绑定到 Angular6 中 UI 中的字段下拉列表
- vb.net - 如何减少pdf中嵌入的重复字体
- c - 在 C 中测量没有 pthread 的开销
- css - 如何在 ODOO 中更改 One2many 背景的颜色?
- angular - 如何获取一个元素的类列表到另一个元素
- javascript - FB.ui 回调从 2018 年 2 月 1 日起不起作用
- javascript - 可以基于 jsonpath 更新 json 的 JavaScript 库或模块是什么?