【发布时间】:2015-10-01 23:20:25
【问题描述】:
我正在编写一个简单的 Web 服务器。这是我的 Python 代码:
import http.server
from os import curdir, sep
class RequestHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
if self.path.endswith(".php"):
f = open(curdir + sep + self.path)
self.send_response(200);
self.send_header("Content-type", "text/php")
self.end_headers()
self.wfile.write(f.read())
f.close()
else:
self.send_error(404, "Cannot open non-html file")
httpd = http.server.HTTPServer(("", 8000), RequestHandler)
httpd.serve_forever()
这是 HTML 代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<input type="button" value="Click me" id="button">
<script>
var buttonOnClick =
function() {
var updateButton =
function(result) {
$("#button").replaceWith("<p>" + result + "<p>");
};
$.get("text.php", updateButton);
};
$("#button").click(buttonOnClick);
</script>
</body>
</html>
文本.php
<?php
echo "Hello!";
?>
当我在 Apache 上运行它时,我得到了预期的结果。按钮变为“您好!”当你点击它。但是,当我在关闭 Apache 的情况下运行它时,页面显示 ERR_CONNECTION_REFUSED 错误。这告诉我我的网页使用 Apache 作为其服务器,而不是我的 Python 代码。我无法弄清楚如何让我的 HTML 和 jQuery 代码使用我的自定义 Web 服务器而不是 Apache。这可能是一个非常简单的问题,但我很难过。
如果我的代码有什么可以改进的地方,请告诉我。
【问题讨论】:
-
自定义服务器在 8000 端口上运行。您是否在 URL 中的服务器名称后添加了
:8000? -
我尝试使用
https://localhost:8000/webprograms/testwebserver/index.html。它给了我一个 SSL 连接错误。
标签: php jquery python apache webserver