按照给定here的示例,我尝试设置一个简单的客户端/网络服务器,使用javascript作为客户端,python作为服务器。但是,我似乎做错了,因为我没有在服务器端获得输出(并且在javascript控制台中没有错误消息)。
完整档案为exampleClient.html
:
<html>
<body>
<script src="jquery.js"></script>
<script>
function toPython(usrdata){
$.ajax({
url: "localhost:8082",
type: "POST",
data: { information : "You have a very nice website, sir." , userdata : usrdata },
dataType: "json",
success: function(data) {
//s <!-- do something here -->
$('#somediv').html(data);
}});
}
$("#filter").click(toPython("something5"));
//$("#onclick").bind('click', toPython("something5"));
</script>
<input type="button" id="filter" name="filter" value="Filter" />
<p id="demo"></p>
</body>
</html>
和server.py
:
# Python and Gevent
from gevent.pywsgi import WSGIServer
from gevent import monkey
monkey.patch_all() # makes many blocking calls asynchronous
def application(environ, start_response):
if environ["REQUEST_METHOD"]!="POST": # your JS uses post, so if it isn't post, it isn't you
start_response("403 Forbidden", [("Content-Type", "text/html; charset=utf-8")])
return "403 Forbidden"
start_response("200 OK", [("Content-Type", "text/html; charset=utf-8")])
r = environ["wsgi.input"].read() # get the post data
print(r)
return r
address = "localhost", 8082
server = WSGIServer(address, application)
server.backlog = 256
server.serve_forever()
我很感激帮助解决问题,或者如果您有另一个工作示例,说明如何创建这样的客户端/服务器设置......