簡體   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