[英]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. 我在本地有两个python服务器,同时托管网站的动态和静态页面。
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: 我正在静态页面上的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.
Can I host these both on the same port, and ideally run a single script to launch both static and dynamic pages? 我可以将它们都托管在同一端口上,并且理想情况下运行单个脚本来启动静态和动态页面吗?
cgiserver.py 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()
wsgiserver.py 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/...). 在具有多个使用同一IP /端口的进程的用例中,应将它们放在第三个进程(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); 考虑到您的问题,我想您处于开发环境中,可能难以设置和配置Web服务器,因此可以使用HTTP代理(找到了SimpleHTTPProxy和ProxyHTTPServer以及相关的文章“ 严重简单的python HTTP代理? ”,但是在网上查看更多); 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; 一般的想法是将第三个程序配置为侦听端口(即80),并根据请求的路径将请求提供给任一后端。 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). 当侦听端口(例如端口80)的进程(例如apache)收到对
/meas
的请求时,如果将其重定向到wsgi服务器(在Unix套接字或端口8081上),则该请求将/cgi-bin
或/htbin
其重定向到CGI处理程序(实际上Apache有一个cgi-bin模块,您可以删除此后端)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.