Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AJAX HTTP请求和Python web服务器HTTP响应返回_Python_Ajax - Fatal编程技术网

AJAX HTTP请求和Python web服务器HTTP响应返回

AJAX HTTP请求和Python web服务器HTTP响应返回,python,ajax,Python,Ajax,我刚刚开始集成ajax http请求并返回python Web服务器http响应。我真的不知道如何使用它 例如,我的网页位于另一个IP上。像192.168.1.1一样,我将从192.168.1.2获得数据或响应 那么在我看来, function test(){ $.ajax({ url : "http://192.168.1.2:8012/", type : "GET", success : function(data) {

我刚刚开始集成ajax http请求并返回python Web服务器http响应。我真的不知道如何使用它

例如,我的网页位于另一个IP上。像192.168.1.1一样,我将从192.168.1.2获得数据或响应 那么在我看来,

function test(){
    $.ajax({
        url : "http://192.168.1.2:8012/",
        type : "GET",
        success : function(data) {
             alert(data);
        }
    });
}
现在在我的pythonweb服务器上

import string,cgi,time
from os import curdir, sep
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import MySQLdb
from lxml import etree
from lxml.builder import E as buildE
import urllib

global db, cnn

db = MySQLdb.connect("localhost","root","password","schema" )
cnn = db.cursor()


class MyHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        global cnn, sql

        self.wfile.write("Cannot GET "+self.path)
        print "test"
        self.send_response(200, "testing")
        self.send_header('Content-type', 'xml')
        self.end_headers()
        self.wfile.write("testing")


    def do_POST(self):
        global rootnode
        try:
            ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
            if ctype == 'multipart/form-data':
                query=cgi.parse_multipart(self.rfile, pdict)
            self.send_response(301)

            self.end_headers()
            upfilecontent = query.get('upfile')
            print "filecontent", upfilecontent[0]
            self.wfile.write("<HTML>POST OK.<BR><BR>");
            self.wfile.write(upfilecontent[0]);

        except :
            pass



def main():
    try:
        server = HTTPServer(('', 8012), MyHandler)
        print 'started httpserver...'
        server.serve_forever()
    except KeyboardInterrupt:
        print '^C received, shutting down server'
        server.socket.close()

if __name__ == '__main__':
    main()
导入字符串,cgi,时间
从操作系统导入curdir,九月
从BaseHTTPServer导入BaseHTTPRequestHandler,HTTPServer
导入MySQLdb
从lxml导入etree
从lxml.builder导入E作为buildE
导入URL库
美国有线电视新闻网全球数据库
db=MySQLdb.connect(“localhost”、“root”、“password”、“schema”)
cnn=db.cursor()
类MyHandler(BaseHTTPRequestHandler):
def do_获得(自我):
全球有线电视新闻网,sql
self.wfile.write(“无法获取”+self.path)
打印“测试”
自我发送_响应(200,“测试”)
self.send_头('Content-type','xml')
self.end_头()
self.wfile.write(“测试”)
def do_POST(自我):
全局根节点
尝试:
ctype,pdict=cgi.parse_头(self.headers.getheader('content-type'))
如果ctype==“多部分/表单数据”:
query=cgi.parse\u多部分(self.rfile,pdict)
self.send_响应(301)
self.end_头()
upfilecontent=query.get('upfile')
打印“文件内容”,upfilecontent[0]
self.wfile.write(“POST OK.

”; self.wfile.write(upfilecontent[0]); 除: 通过 def main(): 尝试: server=HTTPServer(“”,8012),MyHandler) 打印“已启动httpserver…” 服务器。永远为您服务() 除键盘中断外: 打印“^C已收到,正在关闭服务器” server.socket.close() 如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu': main()

我只需要从Web服务器返回数据。但我认为我做错了。

我认为在发送所有标题之前,不应该将数据发送到self.wfile。也许你应该发送一个“内容长度”标题,让你的页面知道什么时候它必须停止等待数据。大概是这样的:

data="Cannot GET "+self.path
self.send_response(200)
self.send_header('Content-type','text/xml')
self.send_header('Content-length',str(len(data))
self.end_headers()
self.wfile.write(data)

那么,你看到了什么?你犯了什么错误?@DanielRoseman没用了,兄弟,我该怎么帮你?“它不工作”是什么意思?你看到了什么?你会犯什么错误?什么是不应该发生的,或者什么不是应该发生的?@DanielRoseman在我的服务器上它确实打印“测试”,但它不会返回到我的ajax并打印数据。