简体   繁体   中英

Hosting CGI and WSGI servers on the same port?

I've got two python servers locally hosting both the dynamic and static pages of a site.

I'm showing the dynamic page in an iframe on a static page, and want to send data to the parent static page using the javascript parent.functionx(data) approach, but I get error:

Uncaught SecurityError: Blocked a frame with origin "http://localhost:8001" from accessing a frame with origin "http://localhost:8000". Protocols, domains, and ports must match.

Can I host these both on the same port, and ideally run a single script to launch both static and dynamic pages?

  1. A WSGI server on port 8001 to host a dynamic python page, accessed by the path /meas

cgiserver.py

import subprocess
from cherrypy import wsgiserver

def application(env, start_response):
    if '/meas' in env['PATH_INFO']:
        start_response('200 OK', [('Content-Type', 'text/html')])
        pathargs = env['PATH_INFO']
        args = pathargs.split('/')
        participantid = args[2]
        studyid = args[3]
        repeatid = args[4]
        proc = subprocess.Popen(['python3', 'cgi-bin/script-meas.py', participantid, studyid, repeatid], stdout=subprocess.PIPE)
        line = proc.stdout.readline()
        while line:
            yield line
            line = proc.stdout.readline()

wsgi_server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 8001), application)
wsgi_server.start()
  1. A CGI server on port 8000 to host static files (of which there are many and complex folder structures)

wsgiserver.py

from http.server import CGIHTTPRequestHandler, HTTPServer

handler = CGIHTTPRequestHandler
handler.cgi_directories = ['/cgi-bin', '/htbin']  # this is the default
server = HTTPServer(('localhost', 8000), handler)
print('Serving on localhost:8000');
server.serve_forever() 

Thank you for clarifying your needs.

In your use-case to have multiple processes using the same IP/port, you should put them behind a third process (apache/nginx/lighttpd/...).

Considering your question, I suppose you are in a developpment environment and it might be hard to setup and configure a webserver, so you could use a HTTP proxy (found SimpleHTTPProxy and ProxyHTTPServer and the related post " seriously simple python HTTP proxy? ", but check the web for more); overall, this solution would not be easier...

The general idea is to configure the third program to listen to a port (ie: 80) and to serve the request to either of the backends depending of the requested path; ie:

When the process (say apache) listening to a port (let's say port 80) get a request to /meas it will redirect it to the wsgi server (either on a Unix socket or on the port 8081), if it is a request to /cgi-bin or /htbin it will redirect it to the CGI handler (actually Apache have a cgi-bin module and you could remove this backend).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM