Error: [WinError 193] The specified file is not a valid Win32 application when attempting to load a CSS file within a Python CGI script

When attempting to run a Python 3.4 CGI script on Microsoft Windows, specifically using Python 3.4 and Windows 7 along with the Python 3 HTTPServer module, I encountered an issue where the script could not load the CSS stylesheet file. The output from my HTTPServer was as follows:

C:\Users\aliv>E:\winService\serv.py
serving at port 8000
127.0.0.1 - - [11/Jan/2015 14:11:24] "GET /cgi-bin/winserv.py HTTP/1.1" 200 -
127.0.0.1 - - [11/Jan/2015 14:11:24] command: C:\Python34\python.exe -u C:\Users
\aliv\cgi-bin\winserv.py ""
...

HTTP Code snippet:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8>
    <title>Message Page</title>
    <link rel="stylesheet" type="text/css" href="./winserv.css" />
...

HTTP Server code:

#!/usr/bin/python3
from http.server import HTTPServer, CGIHTTPRequestHandler

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

PORT = 8000

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

Python CGI script code:

#!/usr/bin/python3
import cgi
import cgitb
cgitb.enable()

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

ff = open('./cgi-bin/winserv.html', mode='r', encoding='utf_8')
aa = ff.read()
print(aa.format(message='!'))
ff.close()

Chrome log message:

Resource interpreted as Stylesheet but transferred with MIME type text/plain: "http://localhost:8000/cgi-bin/winserv.css".

Answer №1

Resolved the issue by moving all files to a new directory, excluding .py files from being stored in the cgi-bin folder. However, I am unsure if this is a permanent and optimal solution.

Answer №2

view image explanation here

Greetings everyone!

I encountered a similar issue while using pycharm. I decided to fix it by reinstalling Python with a repair installation. Then, I navigated to File --> project --> project interpreter and selected the Python file path, just like shown in the attached image below. After that, I closed PyCharm and reopened it, made sure to install any necessary libraries into this path based on my project requirements, and finally ran the program successfully.

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

How can a bootstrap gallery maintain a consistent size despite variations in picture dimensions?

I am currently working on creating an image gallery for our website using the latest version of Bootstrap. However, we are facing an issue with the varying sizes of our images. Each time the gallery switches to a new image, it disrupts the overall size and ...

what's causing the tabs in bootstrap to malfunction

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <link href="http://maxcdn.bootstrapcdn.com/bootst ...

Adding an Icon to a Tab in Ant Design - A Step-by-Step Guide

Is there a way to include an icon before the title of each open tab? I am currently using the antd library for tab creation, which doesn't provide a direct option for adding icons. Here is my code snippet along with a link to the jsfiddle https://jsfi ...

Responsive Image Carousel with Bootstrap

I am facing an issue with the bootstrap carousel. When I upload an image with dimensions of 2000px X 500px to the slider, it looks great on desktop screens. However, when viewed on mobile devices, the slider becomes very small and appears at the top of the ...

Is it possible to set a consistent size for thumbnails while still preserving the aspect ratio?

There are several questions that are somewhat similar, but I can't quite connect them to my specific situation. I managed to create a responsive thumbnail grid with a structure of 4-to-3-to-2 columns (25%-33.33%-50%). Currently, I am using a placeho ...

Is there a bug in Firefox when using the multicolumn columns property with Flexbox?

Could this be a compatibility issue with Firefox? The layout appears fine in Chrome or when I take out columns: 2, then it also displays correctly in Firefox. div { width: 500px; padding: 2rem; display: inline-flex; align-items: baseline; bor ...

Crystal report for VS2010 displays scrollbars on the page

I like to present my report in a div container with overflow:scroll I have placed the crystal report viewer inside the DIV and expect it to stay within the div only, which it does. The div shows scroll bars when the report overflows. However, my page als ...

What methods can I use to achieve a stylish and responsive design for my images?

I have been encountering difficulties styling the images for my website. Despite multiple attempts, I am unable to apply CSS properties such as border-radius and responsive design to the images on my page. The images load fine, but they do not seem to acce ...

Codeigniter experiencing issues with loading CSS file

Currently, I am utilizing codeigniter for a specific project of mine. However, I have encountered an issue related to CSS. The problem seems to be with the loading of CSS files as I keep receiving a 404 error when trying to load them. Surprisingly, all oth ...

Unable to access pip on Python 3.5

Having an issue with running the pip command on Python version 3.5 in Windows 10 64-bit. Whenever I attempt to execute the command, the application window briefly appears and then closes immediately. I have already made an attempt to add the directory to ...

As the browser window is resized, the gap between images widens while the images themselves decrease

Hey there, I'm encountering some trouble with the image links at the top of my page. As I resize the browser or change screen resolutions in media queries using percentages, the images are resizing accordingly. However, I'm noticing that as the i ...

Adjusting the dimensions of a div to accommodate varying text lengths

I'm currently facing an issue where I have multiple divs all sharing the same "Text" class. These divs don't have set width or height values, and are adjusting based on the content inside them, which varies in width. The problem arises when other ...

Arrange SASS files in your application by incorporating Bootstrap

Recently, I've embarked on the journey of working on a complex styling project for a large application. Knowing that Bootstrap 4 offers SASS files, I made the decision to align with that framework. To streamline my workflow, I established the followi ...

Creating a sticky section with Tailwind CSS utility classes

I have a page in my Next.js web app, and I want to make the section highlighted in blue (wrapped in <aside> tag) sticky so that it stays in place while scrolling, with only the main section containing the chart scrolling. The code snippet below is f ...

What is the best way to halt Keyframe Animation once users have logged in?

To enhance user engagement, I incorporated keyframe animation into my login icon on the website. The main objective is to capture the attention of visitors who are not registered users. However, once a user logs in, I intend for the keyframe animation to c ...

What could be causing the closest() method in my JavaScript code to not locate the nearest class?

I've encountered an issue while dynamically creating new classes with input fields in my project. For some reason, when I try to remove a previous row, nothing happens. Can anyone help me troubleshoot this problem? Here is the JavaScript code that I ...

Switch up the Background using .css and jCarousel

I am attempting to create a dynamic gallery using jCarousel and Ajax. The thumbnails are being loaded from a '.txt' file. I am trying to achieve the following effect: When a thumbnail is clicked, the background of the body changes. This action sh ...

Improprove Google PageSpeed score - must resolve

My mobile website is loading incredibly slow, despite my attempts to improve the speed using Google PageSpeed insights. Unfortunately, I'm having trouble interpreting the screenshot and identifying what changes need to be made. Is there anyone who c ...

Why isn't my Bootstrap navbar expanding properly?

I am facing an issue with my toggle menu where it is not dropping down as a solid black block but instead in the center transparent. How can I fix this to make it drop down as a solid block and be positioned to the left? I have a bootstrap navbar that I mo ...

Ways to position a single item in the middle of a multi-column layout

I need help with aligning dynamic images in a 2-column layout. What's the best way to center a single image or collapse the columns when there's only one image? Currently, the single image is always placed in the left column. Is there a CSS-only ...