MySQLと対話するPythonCGIプログラムを作成するにはどうすればよいですか?
PythonCGiスクリプトを使用してアカウントにログインするとします。詳細は次のとおりです
login.html
<html>
<body>
<form action="login.py" method="get">
email: <input type="text" name="e1">
password: <input type="password" name="p1">
<input type="submit" value="register">
</form>
</body>
</html>
login.py
#!C:\Python27\python.exe
import MySQLdb
import cgi
import Cookie
# Open database connection
db = MySQLdb.connect("localhost","root","","student" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
data=cgi.FieldStorage()
a=data.getvalue('e1')
b=data.getvalue('p1')
# Prepare SQL query to fetch a record into the database.
sql = "select id,email,password from user where email='"+a+"' AND password='"+b+"'"
try:
# Execute the SQL command
if(cursor.execute(sql)):
# Commit your changes in the database
db.commit()
c=Cookie.SimpleCookie()
# assign a value
c['mou']=a
# set the xpires time
c['mou']['expires']=24*60*60
# print the header, starting with the cookie
print c
print("Content-type: text/html")
print('''<html>
<head>
<title>Hello Word - First CGI Program</title>
</head>
<body>
<h2>successfully login</h2>
</body>
</html>''')
else:
# Commit your changes in the database
db.commit()
print("Content-type: text/html")
print("<html>")
print("<body>")
print("<h2>fail</h2>")
print("</body>")
print("</html>")
except:
# Rollback in case there is any error
db.rollback()
# disconnect from server
db.close()
-
Pythonプログラムを実行する方法は?
コードを記述したら、コードを実行して実行し、出力を取得する必要があります。プログラムを実行すると、コードが正しく記述され、目的の出力が生成されるかどうかを確認できます。 Pythonプログラムの実行は非常に簡単な作業です。 IDLEで実行 IDLEでPythonプログラムを実行するには、指定された手順に従います- Pythonコードを記述して保存します。 プログラムを実行するには、[モジュールの実行]に移動します または、F5をクリックするだけです。 コマンドラインで実行 Pythonスクリプトファイルは「.py」拡張子で保存されます。 Pythonスクリプトを保存したら
-
PythonでテキストエリアデータをCGIプログラムに渡す
TEXTAREA要素は、複数行のテキストをCGIプログラムに渡す必要がある場合に使用されます。 例 これは、TEXTAREAボックスを含むフォームのHTMLコードの例です- <form action = "/cgi-bin/textarea.py" method = "post" target = "_blank"> <textarea name = "textcontent" cols = "40" rows = "4"> Type your te