SoulMazer 26 Posting Whiz in Training

Ugh, this is quite frustrating. I am writing a series of CGI scripts with Python that all work perfectly until I add in a stylesheet reference. Interestingly, if I put the styling rules inside the CGI script, it runs perfectly; it only throws an error when it is in an external stylesheet. I'm not sure if my "Exec format error: Premature end of script headers" error has to do with Python, so I apologize if this is not in the right forum. Sadly there is no HTML/CSS forum here, so I thought the Python forum would be sufficient.

Anyways, here is some of my Python code:

import cgi

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

print "<html>"
print "<head>"
print "<link href='MyStylesheet.css' rel='stylesheet' type='text/css'>"
print "</head>"
print "<body>"
# Cut out
print "</body>"
print "</html>"

MyStylesheet.css:

body {
    background-color: #000000;
}


 a:link { 
    text-decoration: none;
    line-height: 2.0;
    color: #F1F1F1;
}

 a:visited { 
    text-decoration: none;
    line-height: 2.0;
    color: #F1F1F1;
}

 #colored {
    background: #1B1B1B;
}

What's my problem here? Apache's error log says its my stylesheet, but I cannot see any problems with it. Maybe I just need some fresh eyes to look at it?

Thanks in advance everybody.

EDIT: Well, I guess I solved my own problem. Apparently Apache does not like having CSS files anywhere inside the cgi-bin directory. Moving the stylesheet to a different directory and calling it by its full path did the trick.

Dani AI

Generated

The error shown here is a classic server-side mismatch: Apache treats anything inside a CGI-executing location as a program to run. When a browser requests a stylesheet that lives under that location, the server tries to execute the CSS file instead of serving it as static text; because the CSS is not an executable script the OS refuses to run it (Exec format/ENOEXEC) and Apache reports "Premature end of script headers." For background on how Apache maps ScriptAlias/CGI locations see the official docs: Apache CGI howto and ScriptAlias directive.

Practical options to fix or avoid the problem:

  • Serve static assets from a non-CGI location (preferred): put CSS/JS under the document root or a dedicated static folder.
  • Prevent CGI execution for particular files or a directory by changing server config or .htaccess. Example directives you can use where .htaccess is allowed:
# disable CGI execution for this directory
Options -ExecCGI

# or explicitly stop treating .css files as handled by CGI
<FilesMatch "\.css$">
  SetHandler None
</FilesMatch>

Useful troubleshooting checklist:

  • Request the CSS URL directly (curl or browser dev tools) to see the response and headers.
  • Check Apache error_log for ENOEXEC/Exec format errors and the exact request path.
  • Ensure file permissions: static files should be readable by the web server (e.g., 644) and do not need +x.
  • If you must keep something executable, verify a valid shebang and Unix line endings.

This explains why emitting styles inline from the CGI worked: no separate URL was requested so the server did not attempt to execute a standalone CSS file. The behavior described by matches the standard Apache CGI semantics.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.