简体   繁体   English

如何从Python服务器获取Javascript客户端?

[英]How to GET from Python server to Javascript Client?

Hiyo, yo

I've setup a Python server with a GET request which seems to work, but for some reason I can't send anything back to the requesting client, which is coded in Javascript. 我已经设置了一个带有GET请求的Python服务器,该请求似乎可以正常工作,但是由于某种原因,我无法将任何内容发送回请求的客户端,该客户端均使用Javascript编码。

Python Server: Python服务器:

import time
import BaseHTTPServer


HOST_NAME = 'localhost'
PORT_NUMBER = 8000 


class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_HEAD(s):
        s.send_response(200)
        s.send_header("Content-type", "text/html")
        s.end_headers()

    def do_GET(s):
        print "here i am, in python" #this prints, so i know i'm in here

        """Respond to a GET request."""
        s.send_response(200)
        s.send_header("Content-type", "application/json")
        s.send_header("Access-Control-Allow-Origin", "*")
        s.send_header("Access-Control-Expose-Headers", "Access-Control-Allow-Origin")
        s.send_header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
        s.end_headers()
        s.wfile.write("test1")
        return "test2"

        # If someone went to "http://something.somewhere.net/foo/bar/",
        # then s.path equals "/foo/bar/".
        s.wfile.write("<p>You accessed path: %s</p>" % s.path)
        s.wfile.write("</body></html>")


if __name__ == '__main__':
    server_class = BaseHTTPServer.HTTPServer
    httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
    print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)

    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass

    httpd.server_close()
    print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)

Javascript Client: Javascript客户端:

$.ajax({
    method: "GET",
    url: "http://localhost:8000",
    success:function(result){
        console.log("success"); //does not print
        console.log(result);   //does not print
    },
    failure:function(err){
        console.log("couldn't make it"); //does not print
    }
});

So what I want is to send a GET request from JS to Py, and get some feedback from the server. 因此,我要从JS向py发送GET请求,并从服务器获取一些反馈。 Alas, nothing prints anywhere except for "here i am, in python" , which suggests I'm in the server's GET code, but nothing gets sent back. las,除了"here i am, in python"之外,其他任何地方都不会打印,这表明我在服务器的GET代码中,但是什么也没有发回。

any ideas? 有任何想法吗?

Thanks! 谢谢!

UPDATE 更新

Ok, after tweaking with eton's suggestion, I have "success" printing from the success function. 好的,在调整了eton的建议之后,我从成功功能中获得了“成功”打印。

the result parameter is also being printed, however, it is empty. result参数也将被打印,但是它为空。

the do_GET now looks like this: do_GET现在看起来像这样:

    def do_GET(s):
        print "here i am, in python"
#       """Respond to a GET request.""" 
        s.send_response(200)
        s.wfile.write("test1") 
        s.send_header("Content-type", "text/html")
        # s.send_header("Content-type", "application/json") 
        s.send_header("Access-Control-Allow-Origin", "*") 
        s.send_header("Access-Control-Expose-Headers", "Access-Control-Allow-Origin") 
        s.send_header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept") 
        s.end_headers() 

Maybe I'm returning it wrong? 也许我把它归还错了吗? I have no idea. 我不知道。 Been reading the docs and other posts but to no avail so far. 一直在阅读文档和其他帖子,但到目前为止没有任何结果。

Thanks again 再次感谢

Actually, with this code the JS client will wait indefinitely for a response. 实际上,使用此代码,JS客户端将无限期地等待响应。 You need to set Content-length header to tell the JS client when it should stop waiting for more bytes. 您需要设置Content-length标头,以告知JS客户端何时应停止等待更多字节。

In your case, this translates to this: 在您的情况下,这转换为:

self.send_header("Content-Length", str(len('test1')))

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

相关问题 如何通过ajax从javascript服务器获取换行符并在客户端中执行? - how to get newline from server through ajax as javascript and execute it in client? Javascript客户端从服务器获取响应 - Javascript client get response from server JavaScript:从服务器到客户端 - JavaScript: from server to client 如何从服务器端javascript从不同的域获取.txt文件并获取它的客户端 - How to fetch a .txt file from a different domain from server side javascript and get it client side 如何将python脚本作为服务器与python和javascript应用作为客户端连接? - How to connect python script as a server with python and javascript apps as client? 如何使用Javascript从客户端获取这些信息? - How to get these information from the client with Javascript? 如何使用客户端JavaScript从外部服务器获取Ajax请求 - How to get an Ajax request from an external server using client side JavaScript 如何通过PHP从客户端向服务器获取javascript整理的数据 - How can I get my javascript gererated data from the client to the server via PHP 将捕获的图像从python服务器发送到javascript客户端 - Send captured images from python server to javascript client 网站:将数据从javascript(客户端)发送到python(服务器) - Website: Send data from javascript (client) to python (server)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM