0

I've been tasked with creating a website using both HTML and Python CGI. I've ran into many issues however managed to solve them all and got the HTML and CGI to work on the inbuilt python webserver using the command: python3 -m http.server --cgi -b 127.0.0.1 8000

However, this project is being marked on my universities own webserver, and when I run the code from that webserver, Once the script attempts to open the CGI file, I'm met with this error:Error Referred from: https://project.cs.cf.ac.uk/LootsSG/part202/ HTTP status: 500

Your code returned an error: End of script output before headers: hello_get.py

This really confuses me as I can't figure out what that error means, or why it would only work on particular servers.

HTML code:

<!DOCTYPE html>
<html>

<head>
<link rel="stylesheet" href="style.css">
<meta charset="utf-8">
<title>First Form</title>
</head>
<h3>Would you like it outputted numerically or Verbosely</h3>
<body>
<form class = "border" action="cgi-bin/hello_get.py" method="POST">
<input type="radio" name="Output" id="num" value="num"/> numerically <br />
<input type="radio" name="Output" id="verb" value="verb"/> Verbosely <br>
<input type="radio" name="Output" id="both" value="both"/> Both <br>
<label for="num_alph">choose the option you'd prefer</label>

<h3>Enter a Year to find out when the easter of that year is</h3>
<label for="theYear">Enter the year of the easter date you would like to know</label>
<input type="number" id="theYear"
name="theYear" size="10"/> <br/>
<input type="submit" value="submit"/>
</form>

</body>
</html>

import cgi, cgitb
cgitb.enable()
form = cgi.FieldStorage()
# Numerical = form.getvalue('num')
# Verbose = form.getvalue('verb')
# both = form.getvalue('both')
choice = form.getvalue('Output')
year = form.getvalue('theYear')
year = int(year)
NoApril = False
verbose = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth', 'eleventh', 'twelfth', 'thirteenth', 'fourteenth', 'fifteenth', 'sixteenth', 'seventeenth', 'eighteenth', 'nineteenth', 'twentieth', 'twenty-first', 'twenty-second', 'twenty-third', 'twenty-fourth', 'twenty-fifth', 'twenty-sixth', 'twenty-seventh', 'twenty-eighth', 'twenty-ninth', 'thirtieth']
a = year % 19
b = year % 4
c = year % 7
d = year // 100
e = (13+d*8) // 25
f = d // 4
g = (15 - e + d - f) % 30
h = (4 + d - f) % 7
i = (a*19 + g) % 30
j = (b*2 + c*4 + 6*i + h) % 7
Easter = i + j - 9 
March = 22 + i + j
if Easter <= 0:
    NoApril = True
else:
    if i == 28 and j == 6 and (g*11 + 11) % 30 < 19 and Easter == 25:
        Easter = 18
    elif i == 29 and j == 6 and Easter==26:
        Easter = 19
print('Content-Type: text/html; charset=utf-8')
print('')
print('<!DOCTYPE html>')
print('<html>')
print('<head>')
print('<title>find out the date of eatser on a given year</title>')
print('<link rel="stylesheet" href="/style.css">')
print("<style>")
print("  .body {")
print("    width: 300px;")
print("    padding: 20px;")
print("    margin: 20px;")
print("    border: 1px solid #ccc;")
print("    border-radius: 5px;")
print("    text-align: center;")
print("</style>")
print('</head>')
print('<body>')
print('<p class="body">')
if NoApril:
    VerbMarch = verbose[March-1]
else:
    VerbEaster = verbose[Easter - 1]
if choice == "num":
    if NoApril:
        if March < 10:
            print("In the year " + str(year) + " easter falls on " + "0" + str(March) + "/03/" + str(year) + "<br/>")
        else:
            print("In the year " + str(year) + " easter falls on " + str(March) + "/03/" + str(year) + "<br/>")
    else:
        if Easter<10:
            print("In the year " + str(year) + " easter falls on " + "0" + str(Easter) + "/04/" + str(year) + "<br/>")
        else:
            print("In the year " + str(year) + " easter falls on " + str(Easter) + "/04/" + str(year) + "<br/>")
elif choice == "verb":
    if NoApril:
        print("In the year " + str(year) + " easter falls on the " + VerbMarch + " of March " + str(year) + "<br/>")
    else:
        print("In the year " + str(year) + " easter falls on the " + VerbEaster + " of April " + str(year) + "<br/>")
elif choice == "both":
    if NoApril:
        if March < 10:
            print("In the year " + str(year) + " easter falls on " + "0" + str(March) + "/03/" + str(year) + "<br/>")
        else:
            print("In the year " + str(year) + " easter falls on " + str(March) + "/03/" + str(year) + "<br/>")
    else:
        if Easter<10:
            print("In the year " + str(year) + " easter falls on " + "0" + str(Easter) + "/04/" + str(year) + "<br/>")
        else:
            print("In the year " + str(year) + " easter falls on " + str(Easter) + "/04/" + str(year) + "<br/>")
    if NoApril:
        print("In the year " + str(year) + " easter falls on the " + VerbMarch + " of March " + str(year) + "<br/>")
    else:
        print("In the year " + str(year) + " easter falls on the " + VerbEaster + " of April " + str(year) + "<br/>")
print('</p>')
print('</body>')
print('</html>')

I also have some CSS but I doubt it's relevant to this query. This project has to be functional and working by 3PM today, so any input or help would be greatly appreciated

5
  • Are you aware that cgi is: Deprecated since version 3.11, removed in version 3.13 ? Commented Dec 6, 2024 at 12:27
  • 2
    yes, trust me if I could avoid using CGI in any way I would, I don't understand why we aren't using a framework such as flask Commented Dec 6, 2024 at 12:33
  • so I simply put Content-Type: and it'll work? Is it possible you could write out the exact 2 lines I need> Commented Dec 6, 2024 at 12:34
  • Your code works fine for me using Python3.11/MacOS, after adding a proper shebang line in order to use my venv Python. Commented Dec 6, 2024 at 12:45
  • I managed to figure it out, I had 2 main issues: 1) I also had a CSS file in my CGI-bin folder, 2)I didn't have the shebang line #!/usr/bin/python3. Thankyou for your help anyways Commented Dec 6, 2024 at 12:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.