繁体   English   中英

在同一端口上托管CGI和WSGI服务器?

[英]Hosting CGI and WSGI servers on the same port?

我在本地有两个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 /端口的进程的用例中,应将它们放在第三个进程(apache / nginx / lighttpd / ...)的后面。

考虑到您的问题,我想您处于开发环境中,可能难以设置和配置Web服务器,因此可以使用HTTP代理(找到了SimpleHTTPProxyProxyHTTPServer以及相关的文章“ 严重简单的python HTTP代理? ”,但是在网上查看更多); 总的来说,这个解决方案不会更容易...

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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