在CGI Python中使用HTML表单数据

时间:2018-07-01 16:47:28

标签: python html forms web cgi

  

我正在使用Python CGI脚本来使我的用户注册表单在XAMPP服务器上运行,但这给我一个错误。我也是本网站的新手,因此如果可能,请评论如何使代码和错误信息更具可读性。我必须手动输入双行以提高可读性,因为它删除了所有格式和缩进。

Error is :
  

服务器遇到内部错误,无法完成您的请求。

     

错误消息:

     

标头之前的脚本输出结尾:server.py

     

如果您认为这是服务器错误,请与网站管理员联系。

     

错误500

     

本地主机

     

Apache / 2.4.33(Win32)OpenSSL / 1.1.0h PHP / 7.2.6

HTML文件的代码为:

<body>
    <div id="wrapper">
        <div id="login">
            <h2>Welcome</h2>
            <img src="media/login_icon.png" height="60px" width="auto">
            <form action="server.py" enctype="text/plain" onSubmit="return validation()" method="post" name="signupForm">
                <p>Username :</p>
                <input type="text" size="33" id="userName" placeholder="username here" name="userName" required><span id="userResponse"> </span>
                <p>Password :</p>
                <input type="password" size="33" id="pass" placeholder="password here" name="password" required><span id="passwordResponse"> </span>
                <p>Confirm Password :</p>
                <input type="password" size="33" id="cPass" placeholder="password here" name="cPass" required><span id="cPassResponse"> </span>
                <p>Email :</p>
                <input type="email" size="33" id="email" placeholder="email here" name="email" required><span id="emailResponse"> </span>
                <p>Phone Number :</p>
                <input type="text" size="33" id="pNumber" placeholder="phone number here" name="pNumber" required><span id="pNumberResponse"> </span>
                <br />
                <button type="submit">Sign Up</button>
                <p id="footer">Already Have an Account ?<a href="index.html">Login</a></p>
            </form>
        </div><!--End of Login Div-->   
    </div><!--End of Wrapper Div-->
</body>

并且server.py文件是:

#!C:\Users\tushar\AppData\Local\Programs\Python\Python36-32\python.exe
import cgi

import cgitb

import mysql.connector

cgitb.enable()

def htmlTop():

print("Content-type:text/html\n\n <DOCTYPE html><html lang="en"><head><meta charset="utf-8"/><title> My Server-side template</title></head><body>")

def htmlTail():

  print("</body></html>")



def connectDB():

  my=mysql.connector.connect(host='localhost' , user='root' , password='' , database='signUp')

  c=my.cursor()

  return my,c


def getData():

  formData = cgi.FieldStorage()

  userName = formData.getvalue("userName")

  password = formData.getvalue("password")

  email = formData.getvalue("email")


  pNumber = formData.getvalue("pNumber")

  dataList = (userName,password,email,pNumber)

  return dataList


def createTable(my,c):

  c.execute("CREATE TABLE IF NOT EXISTS signUp (ID INT AUTO_INCREMENT PRIMARY KEY,USERNAME VARCHAR[36] NOT NULL ,PASSWORD VARCHAR[36] NOT NULL , EMAIL VARCHAR[36] NOT NULL , PHONE_NUMBER VARCHAR[36] NOT NULL)")

  my.commit()


def entry(my,c,dataList):

  c.execute("INSERT INTO signUp (USERNAME , PASSWORD , EMAIL , PHONE_NUMBER) VALUES (%s %s %s %s)",dataList)

  my.commit()



try:

        htmlTop()

        db,cursor = connectDB()

        createTable(db,cursor)

        dataList = getData()


        entry(db,cursor,dataList)

        htmlTail()

  except:

        cgi.print_exception()

0 个答案:

没有答案