如何使用CORS请求'POST'到简单的Python服务器?

时间:2016-12-09 14:08:29

标签: javascript python ajax cors

问题

我需要的是从网页运行本地Python脚本,然后让网页获取该Python脚本的输出。到目前为止,我一直试图使用“POST”请求向该Python脚本(来自网页)提供输入参数。如何解决以下错误?

http://localhost:8000/ 501 (Unsupported method ('POST'))
http://localhost:8000/py/scripts/dnds.py 501 (Unsupported method ('POST'))

到目前为止......

我有以下文件系统(为清晰起见,最小):

./vb-genes.html               # Webpage
./py/scripts/dnds.py          # Script takes input args from the webpage
./py/scripts/start_server.py  # Starts the Python server

在打开vb-genes.html网页之前,我执行Python服务器(来自。):

python ./py/scripts/start_server.py

应该从http://localhost:8000和代码

提供
#!/usr/bin/python
from SimpleHTTPServer import SimpleHTTPRequestHandler
import BaseHTTPServer

class CORSRequestHandler (SimpleHTTPRequestHandler):
    def end_headers (self):
        self.send_header('Access-Control-Allow-Origin', '*')
        self.send_header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
        self.send_header('Access-Control-Allow-Headers', '*')
        SimpleHTTPRequestHandler.end_headers(self)

if __name__ == '__main__':
    BaseHTTPServer.test(CORSRequestHandler, BaseHTTPServer.HTTPServer)

然后我打开网页vb-genes.html,从那里我有三个重要的功能:

  1. createCORSRequest

     // Create the XHR object: 
    function createCORSRequest(method, url) {
      var xhr = new XMLHttpRequest();
      if ("withCredentials" in xhr) {
        // XHR for Chrome/Firefox/Opera/Safari.
        xhr.open(method, url, true);
      } else if (typeof XDomainRequest != "undefined") {
        // XDomainRequest for IE.
        xhr = new XDomainRequest();
        xhr.open(method, url);
      } else {
        // CORS not supported.
        xhr = null;
      }
      return xhr;
    }
    
  2. 通过runPy()生成CORS请求以运行本地Python脚本:

    // Make the actual CORS request.
    function makeCorsRequest(qry_seq, ref_seq) {
        var url = 'http://localhost:8000/';  //not entirely sure what to put here, the 
        var xhr = createCORSRequest('POST', url);
        if (!xhr) {
            alert('CORS not supported');
            return;
        }
        // Response handlers. @xhr-body
        xhr.onload = function(qry_seq, ref_seq) {
            // Run the dnds.py calculator
            runPy(qry_seq, ref_seq);    # <--- AJAX request , see: 3.
        };
        xhr.onerror = function() {
            alert('Woops, there was an error making the request.');
        };
        xhr.send();
    }
    
  3. runPy()是一个ajax请求,用于运行本地python脚本:dnds.py

    // dnds.py calculator
    function runPy(qry_seq, ref_seq) {
        $.ajax({
            type: 'POST', 
            url: 'http://localhost:8000/py/scripts/dnds.py', 
            dataType: 'text',
            success: function(response){
                output = response;
                alert(output);
            }
        }).done(function(data){
            console.log(data);
            alert(data);
        });
    };
    
  4. 要重新讨论这个问题......我为什么会这样做?:

    http://localhost:8000/ 501 (Unsupported method ('POST'))
    http://localhost:8000/py/scripts/dnds.py 501 (Unsupported method ('POST'))
    

0 个答案:

没有答案