为什么我的python文件没有获得所有提交的数据?

时间:2018-06-12 04:36:36

标签: python html python-3.x python-2.7 cgi

这是我的html代码,它是一个记录10个响应的表单,并将它们提交给python程序predictions.py

<form method="post" action="predictions.py" style="font-family: courier;" >
  What is you favorite hobby: <input type="text" name="hobby"
                                 placeholder="e.g. biking"
                                 maxlength="20"
                                 autofocus required="required"/>
  <br>
  <br>
  <br>
  What is your favorite word: <input type="text" name="favorite_word"
                                 placeholder="e.g. apple"
                                 maxlength="20"
                                 autofocus required="required"/>
  <br>
  <br>
  <br>
  What is your dream job: <input type="text" name="job"
                                 placeholder="e.g. Fireman"
                                 maxlength="20"
                                 autofocus required="required"/>
  <br>
  <br>
  <br>
  Mac OS or Windows?
  <br>
  Windows<input type="radio" name="os"
                                value="windows"/>
                                <br>
                                Mac OS
                    <input type="radio" name="os"
                                value="mac"/>
  <br>
  <br>
  <br>
  Heavy rain or sunshine? <br>
  Heavy rain<input type="radio" name="weather"
                                value="rain"/>
                                <br>
                                Sunshine
                    <input type="radio" name="weather"
                                value="sunshine"/>
 <br>
 <br>
 <br>
  Amazon or Ebay? <br>
  Amazon<input type="radio" name="shopping"
                                value="amazon"/>
                                <br>
                                Ebay
                    <input type="radio" name="shopping"
                                value="ebay"/>
<br>
<br>
<br>
  Youtube or Netflix? <br>
  Youtube<input type="radio" name="video"
                                value="youtube"/>
                                <br>
        Netflix<input type="radio" name="video"
                                value="netflix"/>
                                <br>
                                <br>
                                <br>
Laptop or Desktop <br>
Laptop<input type="radio" name="computer"
                                value="laptop"/>
                                <br>
                    Desktop<input type="radio" name="computer"
                                value="desktop"/>
                                <br>
                                <br>
                                <br>
Favorite Season<br>
Summer<input type="radio" name="season"
                                value="summer"/>
                                <br>
                   Winter <input type="radio" name="season"
                                value="winter"/>
                                <br>
                    Fall<input type="radio" name="season"
                                value="fall"/>
                                <br>
                    Spring<input type="radio" name="season"
                                value="spring"/>
<br>
<br>
<br>
Favorite Color
<br>
Red
<input type="radio" name="color" value="red"/>
<br>
Orange
<input type="radio" name="color" value="orange"/>
<br>
Yellow
<input type="radio" name="color" value="yellow"/>
<br>
Green
<input type="radio" name="color" value="green"/>
<br>
Blue
<input type="radio" name="color" value="blue"/>
<br>
Indigo
<input type="radio" name="color" value="indigo"/>
<br>
Violet
<input type="radio" name="color" value="violet"/>
<br>
Pink
<input type="radio" name="color" value="pink"/>
<br>
White
<input type="radio" name="color" value="white"/>
<br>
Black
<input type="radio" name="color" value="black"/>
<br>
<br>
<br>
  <input type="submit" name="button" value="Submit everything" />
</form>

python程序应该将所有提交的信息分配给变量。

def getData():
formData = cgi.FieldStorage()
color = formData.getvalue('color')
season = formData.getvalue('season')
computer = formData.getvalue('computer')
video = formData.getvalue('video')
shopping = formData.getvalue('shopping')
weather = formData.getvalue('weather')
os = formData.getvalue('os')
favorite_word = formData.getvalue('favorite_word')
hobby = formData.getvalue('hobby')
job = formData.getvalue('job')

但它无法正常工作,我不断收到内部服务器错误。

继承完整的python脚本:

#!/usr/bin/python
import cgi


def getData():
    formData = cgi.FieldStorage()
    color = formData.getvalue('color')
    season = formData.getvalue('season')
    computer = formData.getvalue('computer')
    video = formData.getvalue('video')
    shopping = formData.getvalue('shopping')
    weather = formData.getvalue('weather')
    os = formData.getvalue('os')
    favorite_word = formData.getvalue('favorite_word')
    hobby = formData.getvalue('hobby')
    job = formData.getvalue('job')


outcomes = {-10: 'Your end is near', -9: 'I forsee a tragedy that will destroy your soul', -8: 'Consider checking for cancer', -7: 'Your oven might explode or something', -6: 'You are goign to get scammed', -5: 'You might break a bone or two', -4: 'You will have a bad case of food poisoning', -3: 'You failed that test you just took', -2: 'Your toilet is goign to clog', -1: 'You will lose 20 dollars', 0: 'Damn your life is boring', 1: 'You will find 20 dollars on the floor', 2: 'Your teacher will be giving extra credit the next test', 3: 'You got a 100 on the test you just took due to a grading error', 4: 'You are going to win the next raffle you join', 5: 'You will win a lifetime supply of a food you like', 6: 'The bank will accidently transfer 10k to your account and never reverse it', 7: 'You will be accepted into every university you apply to', 8: 'You will win 1 million dollars on your next lawsuit', 9: 'You will live to 150', 10: 'You are going to be the worlds first trillionaire'}


color_vals = {'red': 1, 'orange': 1, 'yellow': 0, 'green': -1, 'blue': 0, 'indigo': -1, 'violet': 0, 'pink': 1, 'white': -1, 'black': -1}
season_vals = {'summer': 1, 'winter': -1, "fall": 0, 'spring': 0}
computer_vals = {'laptop': 1, 'desktop': -1}
video_vals = {'youtube': 1, 'netflix': -1}
shopping_vals = {'amazon': -1, 'ebay': 1}
weather_vals = {'sunshine': -1, 'rain': 1}
os_vals = {'mac': -1, 'windows': 1}


def calc():
    abc = 'abcdefghijklmn'
    score = 0
    score += color_vals[color]
    score += season_vals[season]
    score += computer_vals[computer]
    score += video_vals[video]
    score += shopping_vals[shopping]
    score += weather_vals[weather]
    score += os_vals[os]
    if job[0] in abc:
        score += 1
    else:
        score -= 1
    if len(favorite_word) <= 7:
        score += 1
    else:
        score -= 1
    if hobby[-1] not in abc:
        score += 1
    else:
        score -= 1
    fate = outcomes[score]
    return fate


def head():
    print '''Content-type:text/html\n\n
            <!DOCTYPE html>
                <html>
                <head> <title> Your Future </title> </head>

                <body bgcolor="#ffffcc">
                <h1 style="text-align: center; font-family: courier; ">Our prediction/advice </h1>'''


def tail():
    print '</body> </html>'


def main():
    getData()
    head()
    print '<h1 style="text-align: left; font-family: courier; ">''' + calc() + '</body> </html>'
    tail()


if __name__ == "__main__":
    try:
        main()
    except:
        cgi.print_exception()

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

只需将其置于顶部并尝试:

#!/usr/bin/env python 
import cgitb
cgitb.enable()