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

Passing Information using GET method ใน Python

วิธี GET ส่งข้อมูลผู้ใช้ที่เข้ารหัสต่อท้ายคำขอหน้าเว็บ หน้าเว็บและข้อมูลที่เข้ารหััดแยกด้วยตัวอักษร์ ? ดังนี้:

https://www.test.com/cgi-bin/hello.py?key1=value1&key2=value2

GET method เป็นวิธีเริ่มต้น (default) ในการส่งข้อมูลจากเบราว์เซอร์ไปเว็บเซิร์ฟเวอร์ และสร้างสตริงยาวที่ปรากฏใน Location box ของเบราว์เซอร์ อย่าใช้ GET method ถ้ามี password หรือข้อมูลละเอียดอ่อนอื่นๆ

GET method มีข้อจำกัดขนาด: ส่งได้แค่ 1024 ตัวอักษรใน request string GET method ส่งข้อมูลผ่าน QUERY_STRING header และเข้าถึงได้ใน CGI Program ผ่านตัวแปรสภาพแวดล้อม QUERY_STRING

คุณสามารถส่งข้อมูลโดยการต่อ key-value pairs ไปกับ URL หรือใช้แท็ก HTML <FORM> เพื่อส่งข้อมูลด้วย GET method

ตัวอย่าง URL 単純: GET Method

URL 単純ที่ส่งค่าสองค่าไปยัง hello_get.py ด้วย GET 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 ""

ผลลัพธ์ตัวอย่าง

Hello ZARA ALI

ตัวอย่าง FORM 単純: GET Method

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

First Name:
Last Name:

ผลลัพธ์

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

Passing Information using GET method ใน Python