0
votes

I don't understand why this code gives me a white page in my browser. This is my script:

# 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()

The HTML page to wich it refers is:

<!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>

All I got is a white browser page. And this is the feedback of the server:

"C:\Users\Dell E6430\AppData\Local\Programs\Python\Python35\python.exe" "E:/Anton/Python scripts/H8_webserver_cgi_run_v2.py" Starting simple_httpd on port: 8080 cgi-dirs: ['/H8_cgi'] try: http://localhost:8080/H8_cgi/test1.html First the browser, later the server. How can this possibly work? 127.0.0.1 - - [03/Jun/2018 18:20:18] "GET /H8_cgi/test1.html HTTP/1.1" 200 -

127.0.0.1 - - [03/Jun/2018 18:20:18] command: E:\Anton\H8_cgi\test1.html ""

Exception happened during processing of request from ('127.0.0.1', 52021)

Traceback (most recent call last):

File "C:\Users\Dell E6430\AppData\Local\Programs\Python\Python35\lib\socketserver.py", line 313, in _handle_request_noblock self.process_request(request, client_address) File "C:\Users\Dell E6430\AppData\Local\Programs\Python\Python35\lib\socketserver.py", line 341, in process_request self.finish_request(request, client_address) File "C:\Users\Dell E6430\AppData\Local\Programs\Python\Python35\lib\socketserver.py", line 354, in finish_request self.RequestHandlerClass(request, client_address, self) File "C:\Users\Dell E6430\AppData\Local\Programs\Python\Python35\lib\socketserver.py", line 681, in init self.handle() File "C:\Users\Dell E6430\AppData\Local\Programs\Python\Python35\lib\http\server.py", line 422, in handle self.handle_one_request() File "C:\Users\Dell E6430\AppData\Local\Programs\Python\Python35\lib\http\server.py", line 410, in handle_one_request method() File "C:\Users\Dell E6430\AppData\Local\Programs\Python\Python35\lib\http\server.py", line 645, in do_GET f = self.send_head() File "C:\Users\Dell E6430\AppData\Local\Programs\Python\Python35\lib\http\server.py", line 953, in send_head return self.run_cgi() File "C:\Users\Dell E6430\AppData\Local\Programs\Python\Python35\lib\http\server.py", line 1161, in run_cgi env = env File "C:\Users\Dell E6430\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 676, in init restore_signals, start_new_session) File "C:\Users\Dell E6430\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 955, in _execute_child startupinfo) OSError: [WinError 193] %1 is geen geldige Win32-toepassing

The HTML document is in the location E:\Anton\H8_cgi\test1.html The same location as the script.

How can I let this thing work?

1

1 Answers

0
votes

I have got the answer about the white page. The path in CGIHTTPRequestHandler.cgi_directories=['/H8_cgi'] was not explicit. I did change it in to CGIHTTPRequestHandler.cgi_directories=['E:\Anton\H8_cgi\']. So a absolute path instead of a relative path works.