We've made a form and when the user fills out the form and clicks "send" we want to do the following:
1) Get the data
2) Run a python script which displays the factorial of 10

<!test get form>
<html>
<head>
<center>
TEST
</center>
</head>
<body>
<form name="input" action="./fact.py" method="get">
Enter Text: <input type="text" name="txtfield"><br/>
<input type="radio" name="radio" value="true">
<input type="submit" value="send">
</form>
</body>
</html>

But when we click on send, instead of running the file, it downloads it. Any ideas why?

Dani AI

Generated

— what you describe (browser downloading fact.py) usually means the webserver is treating the file as a static download instead of executing it. was correct that seeing the Python helps, but since your script runs from the terminal the likely culprits are server configuration or script setup (not the factorial logic itself).

Quick checklist to fix and test:

  1. Ensure the script has a proper shebang as the very first line (for example #!/usr/bin/env python3).
  2. The script must emit an HTTP header before any body text, e.g. Content-Type: text/html followed by a blank line.
  3. Make the file executable (chmod 755 fact.py) and place it in a CGI-enabled location (cgi-bin) or enable CGI for the directory (Apache: Options +ExecCGI + AddHandler cgi-script .py in config or .htaccess). On IIS you must map .py to a CGI/FastCGI handler.
  4. Check server error logs and use the browser DevTools Network tab to inspect response headers (if the server returns application/octet-stream or no headers it explains the download). Also watch for CRLF problems if the file was edited on Windows.

A minimal CGI test (drop in cgi-bin, mark executable) to confirm the server will run Python:

#!/usr/bin/env python3
print("Content-Type: text/html")
print()
import math
print("<html><body>Factorial of 10 is", math.factorial(10), "</body></html>")

If this prints in the browser then integrate your real script; if it still downloads, the host likely isn’t configured to execute .py as CGI — contact the host or enable the handler. For reference, see the Python CGI docs and the Apache CGI howto: Python CGI docs Apache CGI howto.

Recommended Answers

All 3 Replies

try putting this question on the programming thread. without seeing the python code, there's no way to tell what's wrong.

try putting this question on the programming thread. without seeing the python code, there's no way to tell what's wrong.

The python code works perfectly when we try to execute it in the terminal window.

yes, but it's not an HTML question.

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.