Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Passing Information Using POST Method ใน Python

วิธีที่น่าเชื่อถือกว่าสำหรับการส่งข้อมูลไปยัง CGI program คือวิธี POST วิธีนี้บรรจุข้อมูลในลักษณะเดียวกับ GET methods แต่แทนที่จะส่งเป็นสตริงข้อความหลัง ? ใน URL มันส่งเป็นข้อความแยกต่างหาก ข้อความนี้เข้าสู่ CGI script ในรูปของ standard input

ตัวอย่างโค้ด

ด้านล่างคือสคริปต์ hello_get.py เดียวกันที่จัดการทั้ง GET และ POST method:

#!/usr/bin/python
# Import modules for CGI handling
import cgi, cgitb

# Create instance of FieldStorage
form = cgi.FieldStorage()

# Get data from fields
first_name = form.getvalue('first_name')
last_name = form.getvalue('last_name')

print "Content-type:text/html\r\n\r\n"
print ""
print ""
print "Hello - Second CGI Program"
print ""
print ""
print "

Hello %s %s

" % (first_name, last_name) print "" print ""

ผลลัพธ์

ตัวอย่างเดิมที่ส่งค่าสองค่าโดย HTML FORM และปุ่ม submit ใช้ CGI script เดียวกันคือ hello_get.py

First Name:
Last Name:

นี่คือผลลัพธ์จริงของฟอร์มข้างต้น คุณใส่ First และ Last Name แล้วกด submit เพื่อดูผลลัพธ์

Passing Information Using POST Method ใน Python