How to start a Python server with parameters on another Python server

问题内容:

So my problem is that I have a server which returns a JSONobject to the user when the user sends a string to its URL-adress. The problem is that this server needs the parameters:
hostname, port (when it is started)
from the users pc in order to serve the user with the JSONobjects.

So I will first have to start a server manually (we can call it getserver1) and the code of the server is (Don’t care about the import-stuff, its just to enable it on both python 2 and 3):

#!/usr/bin/env python
try:
    # Python 3
    from http.server import HTTPServer, SimpleHTTPRequestHandler, test as test_orig
    import sys
    def test (*args):
        test_orig(*args, port=int(sys.argv[1]) if len(sys.argv) > 1 else 8000)
except ImportError: # Python 2
    from BaseHTTPServer import HTTPServer, test
    from SimpleHTTPServer import SimpleHTTPRequestHandler

class CORSRequestHandler (SimpleHTTPRequestHandler):
    def end_headers (self):
        self.send_header('Access-Control-Allow-Origin', '*')
        SimpleHTTPRequestHandler.end_headers(self)

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

On this server I have put a html-file with the code:

<html>
    <head>
        <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    </head>

    <body>
        <script>        
            $.ajax({
               url: "http://user-pc:8000/getserver2.py",
               success: function(response) {
                    console.log(response);
               }
            });

        </script>       
    </body>
</html>

The error I get is the: No “Access-Control-Allow-Origin”
when the html file is run. The code of the getserver2.py has the same code as the getserver1 above which should allow the “Access-Control-Allow-Origin”.

So how can I get this to work. I would like to not involve any new elements as php or something like that into the stew.

Also, the next problem when this works is to start the getserver2 with the 2 parameters (So yes getserver2 is a little different from getserver1 but only in the sense that it takes the two parameters). But the test I’m doing right now uses the same code in both servers to start with.

问题评论:

原文地址:

https://stackoverflow.com/questions/47754487/how-to-start-a-python-server-with-parameters-on-another-python-server

添加评论

友情链接:蝴蝶教程