为什么这个python CGI脚本会给出一个白页?

时间:2018-06-03 16:56:15

标签: python html cgi

我不明白为什么这段代码会在浏览器中为我提供一个白页。 这是我的剧本:

# H8_webserver_cgi_run_v2.py
# Before the server is running:
# http://localhost:8080/H8_cgi/test-1.py

from http.server import HTTPServer, CGIHTTPRequestHandler
## cgi_directories
## This defaults to
['/cgi-bin', '/htbin'] 
CGIHTTPRequestHandler.cgi_directories=['/H8_cgi']
port = 8080
httpd = HTTPServer(('127.0.0.1', port), CGIHTTPRequestHandler)
print("Starting simple_httpd on port: " + str(httpd.server_port))
print("cgi-dirs: ",CGIHTTPRequestHandler.cgi_directories)
print('try: http://localhost:8080/H8_cgi/test1.html')
import webbrowser
webbrowser.open_new_tab('http://localhost:8080/H8_cgi/test1.html')
# First the browser, later the server. How can this possibly work? 
print('First the browser, later the server. How can this possibly work?')
httpd.serve_forever()

它引用的HTML页面是:



<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <!-- Show nicely on phones and tablets -->
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
        <title>H8: Simpele CGI-demo</title>
    </head>
    <body>
        <h1>Een formulier verwerken met CGI. </h1>
            Vul het formulier in.
        <form action = "http://localhost:8080/H8_cgi/test-2.py" method="post">

  <!-- door <pre> te gebruiken hoeven we ons niet te bekommeren over html-opmaak -->
  <pre><hr />
  Uw geslacht              <input type="radio" name="Geslacht" value="man" > Man<br>
                           <input type="radio" name="Geslacht" value="vrouw" checked> Vrouw<br>
  I love Python            <input type="checkbox" name="Python" value="I love Python" />
  Ik kan Python dromen     <input type="checkbox" name="Python" value="Ik kan Python dromen" />
  Ik ben een Pythonista    <input type="checkbox" name="Python" value="Ik ben een Pythonista" />
  Uw naam                  <input type="text" name="Uw naam" value="I.N Cognito" />
  Uw auto(s)
      <select name="Uw auto(s)" multiple >
         <option value="geen">----- Geen ----</option>
         <option value="Volvo">Volvo         </option>
         <option value="Saab">Saab           </option>
         <option value="Opel">Opel           </option>
         <option value="Audi">Audi           </option>
         <option value="VW">VW               </option>
         <option value="anders">Anders       </option>
      </select>
<hr /></pre>
          <input type="submit" value="Klaar" />
        </form>

    </body>
</html>
&#13;
&#13;
&#13;

我得到的只是一个白色的浏览器页面。这是服务器的反馈:

&#34; C:\ Users \ Dell E6430 \ AppData \ Local \ Programs \ Python \ Python35 \ python.exe&#34; &#34; E:/ Anton / Python脚本/ H8_webserver_cgi_run_v2.py&#34; 在端口上启动simple_httpd:8080 cgi-dirs:[&#39; / H8_cgi&#39;] 尝试:http://localhost:8080/H8_cgi/test1.html 首先是浏览器,后来是服务器。这怎么可能有效呢? 127.0.0.1 - - [03 / Jun / 2018 18:20:18]&#34; GET /H8_cgi/test1.html HTTP / 1.1&#34; 200 -

127.0.0.1 - - [03 / Jun / 2018 18:20:18]命令:E:\ Anton \ H8_cgi \ test1.html&#34;&#34;

处理来自(&#39; 127.0.0.1&#39;,52021)的请求时发生异常

Traceback(最近一次呼叫最后一次):

文件&#34; C:\ Users \ Dell E6430 \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ socketserver.py&#34;,第313行,在_handle_request_noblock中     self.process_request(request,client_address)   在process_request中的文件&#34; C:\ Users \ Dell E6430 \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ socketserver.py&#34;,第341行     self.finish_request(request,client_address)   文件&#34; C:\ Users \ Dell E6430 \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ socketserver.py&#34;,第354行,在finish_request中     self.RequestHandlerClass(request,client_address,self)   文件&#34; C:\ Users \ Dell E6430 \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ socketserver.py&#34;,第681行, init     self.handle()   文件&#34; C:\ Users \ Dell E6430 \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ http \ server.py&#34;,第422行,句柄     self.handle_one_request()   在handle_one_request中的文件&#34; C:\ Users \ Dell E6430 \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ _ http \ server.py&#34;,第410行     方法()   文件&#34; C:\ Users \ Dell E6430 \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ http \ server.py&#34;,第645行,在do_GET中     f = self.send_head()   send_head中的文件&#34; C:\ Users \ Dell E6430 \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ _ http \ server.py&#34;,第953行     return self.run_cgi()   在run_cgi中的文件&#34; C:\ Users \ Dell E6430 \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ _ http \ server.py&#34;,第1161行     env = env   文件&#34; C:\ Users \ Dell E6430 \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ subprocess.py&#34;,第676行, init     restore_signals,start_new_session)   文件&#34; C:\ Users \ Dell E6430 \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ subprocess.py&#34;,第955行,在_execute_child中     STARTUPINFO) OSError:[WinError 193]%1是geen geldige Win32-toepassing

HTML文档位于E:\ Anton \ H8_cgi \ test1.html位置与脚本位置相同。

我怎么能让这件事有效?

1 个答案:

答案 0 :(得分:0)

我得到了关于白页的答案。 CGIHTTPRequestHandler.cgi_directories = ['/ H8_cgi']中的路径不明确。我确实将其更改为CGIHTTPRequestHandler.cgi_directories = ['E:\ Anton \ H8_cgi \']。所以绝对路径而不是相对路径是有效的。