A guide to setting up a Python CGI web server on a Windows operating system

I am looking to set up a basic Python server on a Windows system to test CGI scripts that will ultimately run on a Unix environment. However, when I access the site, it displays a blank page with no source code visible. I am struggling to identify the issue.

Server

from http.server import HTTPServer, CGIHTTPRequestHandler

class Handler(CGIHTTPRequestHandler):
    cgi_directories = ["/cgi-bin"]

PORT = 8080

httpd = HTTPServer(("", PORT), Handler)
print("serving at port", PORT)
httpd.serve_forever()
httpd.serve_forever()

App:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import cgi, cgitb
form = cgi.FieldStorage() 

# Get data from fields
first_name = form.getvalue('first_name')
last_name  = form.getvalue('last_name')

print ("Content-type:text/html\r\n\r\n")
print ("<html>")
print ("<head>")
print ("<title>Hello - Second CGI Program</title>")
print ("</head>")
print ("<body>")
print ("<h2>Hello %s %s</h2>" % (first_name, last_name))
print ("</body>")
print ("</html>")

When I try to view the program in my web browser, the output is a downloadable file with its name. Although the file seems to have executed, I am not receiving a functional website. What could be causing this issue?

Answer №1

As soon as I hit post, the solution dawned on me. Instead of using \r\n\r\n, all I needed was a simple \n\n.

print ("Content-Type: text/html\n\n")

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to execute a Windows command line that has been escaped within Python

When I execute the given command in a Windows command prompt, it works flawlessly. java -jar "C:\Program Files (x86)\SnapBackup\app\snapbackup.jar" main However, when I attempt to run the same command within a Python script, it fails. ...

Retrieving Base64 Images in Python Selenium: Step-by-Step Guide

Trying to fetch base64 captcha images using Python Selenium has been a challenge. The issue I'm encountering is that I can only access the HTML right before the images are loaded. Here are the steps I've taken: # importing necessary packages f ...

AngularJS: When the expression is evaluated, it is showing up as the literal {{expression}} text instead of

Resolved: With the help of @Sajeetharan, it was pinpointed that the function GenerateRef was faulty and causing the issue. Although this is a common query, I have not had success in resolving my problem with displaying {{}} to show the outcome. I am atte ...

Generating a new set of data by looping through the existing values to create a new column

I need help creating a new column in my data frame to indicate if the game ended in a home victory, away victory or a draw. Here is the code I am currently using: for index, row in df2.iterrows(): if row['home_goals'] > row['away_goa ...

Saving the name and corresponding value of a selected option element into separate fields using Angular framework

I have implemented the following code to generate a select field. The ngModel is successfully updating the value, but I am also looking to capture the name of the selected option and store it in a different field. Is there a solution to achieve this? ( & ...

Locate the data attribute and store it in the database using an ajax request

I am in need of a proper script to insert a data attribute into the database upon clicking using ajax. HTML: <li><a data-cat="random name">Button</a></li> jQuery: $('li a').click(function() { $(this).data('cat&a ...

How can I center two images using a hover effect in WordPress?

When the mouse hovers over an image, it changes to another image. Everything works smoothly except for one issue - the image is not centered. Below is the code I am currently using: HTML: <figure> <a> <img class="hover" src="na ...

Having issues with the <ul> tag not displaying correctly

I'm currently working on implementing a side navbar for my website, but I've run into an issue with spacing. In the demo linked in my fiddle, you'll notice that the <ul> element inside the receiptDropDown class doesn't push the k ...

"Error encountered when attempting to execute the delete() function in Django

Having some trouble with my delete function. It successfully deletes the object but does not redirect to window.location as expected. Instead, I'm getting an error message: DoesNotExist at /api/personnel/delete/ Resource matching query does not exist ...

Tips for navigating directly to the final page of a paginated Flask-Admin view without needing to sort in descending order

In my Python Flask-Admin application for managing database tables, I am looking to have the view automatically start on the last page of the paginated data. It is important to note that I cannot simply sort the records in descending order to achieve this. ...

Targeting an HTML form to the top frame

Currently, I have a homepage set up with two frames. (Yes, I am aware it's a frame and not an iFrame, but unfortunately, I cannot make any changes to it at this point, so I am in need of a solution for my question.) <frameset rows="130pt,*" frameb ...

Restoring a file from archive in Amazon Web Services S3

As I work on a project, my current task involves uploading a collection of JSON files to Amazon S3 for mobile clients to access. To potentially lower the expenses related to individual file transfers, I am pondering whether it is achievable to unzip a fil ...

Tips for accessing an IP camera with OpenCV

Having trouble reading an IP camera stream. Currently, I can only view the video through Internet Explorer because of the ActiveX plugin requirement. The camera is located at 192.168.0.8:8000. Take a look at the image below https://i.stack.imgur.com/kvmA9. ...

"Unlocking the Power of Pandas Dataframes: A Guide to Parsing MongoDB

I have a piece of code that exports JSON data from a MongoDB query like this: querywith open(r'/Month/Applications_test.json', 'w') as f: for x in dic: json.dump(x, f, default=json_util.default) The output JSON looks like this: ...

What is the best method for extracting individual JSON objects from a response object and presenting them in a table using Angular?

After receiving a JSON Array as a response Object from my Java application, I aim to extract each object and display it on the corresponding HTML page using TypeScript in Angular. list-user.component.ts import { HttpClient } from '@angular/common/h ...

Is there a way to decrease the speed of a text when hovering over it?

Is there a way to create a button that displays text before another text only on hover, with a delay? I've managed to achieve the effect but can't figure out how to slow it down. For example, notice how quickly the word "let's" appears when ...

Looking for a solution to fixing the Dlib error in Visual Studio C++?

Encountered an issue while attempting to install dlib using the command "pip install dlib" on Windows. The error message states:</br> ------------------------------------------------------------------------------</br> You must ...

python transforming array

Greetings! Is there a way to efficiently convert the list a = ['1', '2', '3', '4'] into [1, 2, 3, 4] using only one line of Python code? ...

Use jQuery.ajax() to submit data in separate rows of a table

At the moment, I have no issues submitting my form as long as it contains only one row in the table. However, when I add a second row, an error occurs and the submission fails. My goal is to be able to post the data from each table row separately with just ...

Tips for efficiently transforming a C++ vector into a numpy vector in Cython without excessive use of Python interpreter

Specifically: How can I define a function output data type as numpy.ndarray? Is it possible to utilize cimport numpy instead of import numpy in order to create an array without Python overhead? If the line cdef numpy.ndarray array(int start, int end) ...