【发布时间】: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.
我可以将它们托管在同一个端口上,并且最好运行一个脚本来启动静态和动态页面吗?
- 端口 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()
- 端口 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.postMessagefound here。 -
但如果可能的话,我仍然希望将这些托管在一起.. 使服务器启动稍微简单一些