簡體   English   中英

如何從Python服務器獲取Javascript客戶端?

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

yo

我已經設置了一個帶有GET請求的Python服務器,該請求似乎可以正常工作,但是由於某種原因,我無法將任何內容發送回請求的客戶端,該客戶端均使用Javascript編碼。

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客戶端:

$.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
    }
});

因此,我要從JS向py發送GET請求,並從服務器獲取一些反饋。 las,除了"here i am, in python"之外,其他任何地方都不會打印,這表明我在服務器的GET代碼中,但是什么也沒有發回。

有任何想法嗎?

謝謝!

更新

好的,在調整了eton的建議之后,我從成功功能中獲得了“成功”打印。

result參數也將被打印,但是它為空。

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() 

也許我把它歸還錯了嗎? 我不知道。 一直在閱讀文檔和其他帖子,但到目前為止沒有任何結果。

再次感謝

實際上,使用此代碼,JS客戶端將無限期地等待響應。 您需要設置Content-length標頭,以告知JS客戶端何時應停止等待更多字節。

在您的情況下,這轉換為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM