【问题标题】:Hosting CGI and WSGI servers on the same port?在同一个端口上托管 CGI 和 WSGI 服务器?
【发布时间】:2015-10-15 14:47:16
【问题描述】:

我有两台 Python 服务器在本地托管网站的动态和静态页面。

我在静态页面上的 iframe 中显示动态页面,并希望使用 javascript parent.functionx(data) 方法将数据发送到父静态页面,但出现错误:

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.

我可以将它们托管在同一个端口上,并且最好运行一个脚本来启动静态和动态页面吗?

  1. 端口 8001 上的 WSGI 服务器,用于托管动态 python 页面,通过路径 /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. 端口 8000 上的 CGI 服务器,用于托管静态文件(其中有许多复杂的文件夹结构)

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

【问题讨论】:

  • 你能开发“努力让页面互相交谈”吗?因为一个端口(在一个 IP 上)一次只能被一个进程绑定。
  • 当然!我已经澄清了问题的第一部分。谢谢
  • 我找到了一个解决方法,使用 parent.postMessage found here
  • 但如果可能的话,我仍然希望将这些托管在一起.. 使服务器启动稍微简单一些

标签: python cgi wsgi


【解决方案1】:

感谢您阐明您的需求。

在您的用例中有多个进程使用相同的 IP/端口,您应该将它们放在第三个进程(apache/nginx/lighttpd/...)之后。

考虑到您的问题,我想您在开发环境中,可能很难设置和配置网络服务器,因此您可以使用 HTTP 代理(找到 SimpleHTTPProxyProxyHTTPServer 以及相关帖子“@987654323 @",但请查看网络了解更多信息);总的来说,这个解决方案不会更容易......

一般的想法是配置第三个程序监听一个端口(即:80)并根据请求的路径将请求提供给任一后端;即:

当监听端口(比如端口 80)的进程(比如 apache)收到对 /meas 的请求时,它将重定向到 wsgi 服务器(在 Unix 套接字上或在端口 8081 上),如果它是对 /cgi-bin/htbin 的请求,它将重定向到 CGI 处理程序(实际上 Apache 有一个 cgi-bin 模块,您可以删除此后端)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-12
    • 2013-07-12
    • 2014-08-12
    • 2020-08-09
    • 1970-01-01
    • 2020-02-12
    • 2014-06-16
    • 2020-08-10
    相关资源
    最近更新 更多