1

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() 
Ian
  • 1,427
  • 1
  • 15
  • 27
  • 1
    Could you develop "struggling to get pages talk to eachother"? because a port (on an IP) can only be bind only by one process at a time. – bufh Jul 24 '15 at 20:18
  • Sure! I've clarified the first part of the question. Thanks – Ian Jul 24 '15 at 20:27
  • I've found a work-around, by using `parent.postMessage` found [here](http://stackoverflow.com/questions/13042615/allowing-a-child-iframe-to-call-a-function-on-its-parent-window-from-a-different). – Ian Jul 24 '15 at 20:47
  • But I'd still like to host these together, if possible.. to make server launching slightly simpler – Ian Jul 24 '15 at 20:48

1 Answers1

0

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

Community
  • 1
  • 1
bufh
  • 3,153
  • 31
  • 36