联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> Python编程Python编程

日期:2020-04-17 11:06

Before you turn this problem in, make sure everything runs as expected. First, restart the kernel (in the

menubar, select Kernel$\rightarrow$Restart) and then run all cells (in the menubar, select

Cell$\rightarrow$Run All).

Late submission leads a zero.

Make sure you fill in any place that says YOUR CODE HERE or "YOUR ANSWER HERE", as well as your student ID

(starting with 1), name, and section number below:

CGI Programming

© 2018-current, authors at Computer Science and Technology, Division of Science and Technology, BNU-HKBU

United International College

What is CGI

The Common Gateway Interface, or CGI, is a standard for external gateway programs to interface with

information servers such as HTTP servers.

When we enter a URL to request a page from the web Server, it finds that file then sends it back to the

browser.

In CGI programming, instead of sending a file back, it is executed as a program, and the program outputs

is sent back to your browser.

The programs are called CGI scripts. These CGI programs can be a Python Script, PERL Script, Shell Script,

C or C ++ program, etc.

STUDENT_ID = ""

CHINESE_NAME = ""

SECTION_NUMBER = ""

First CGI program

In order to make sure our web server is running correctly, let's configure CGI scripts in two steps.

Find the httpd.conf file under C:\xampp\apache\conf, search for line <Directory "C:/xampp/cgi-bin/" >,

and then use the following code to replace that between <Directory "C:/xampp/cgi-bin/" > and

</Directory> .

Find the httpd.conf file under C:\xampp\apache\conf, search for line AddHandler cgi-script, and add

.py to the end.

Run the first CGI script

Maintain the hello.py file under the XAMPP\cgi-bin as follows. Change the first line to the path of your

python.exe. Remeber that:

you should run the following code on your local machine instead of jupyter note directly

the first comment of the code following is essential, and it includes an address which points to the

directory of the file, 'python.exe', in your computer.

# The first step

<Directory "C:/xampp/cgi-bin/" >

AllowOverride All

Options All

Order allow,deny

Allow from all

</Directory >

# The second step

AddHandler cgi-script .py

In a browser, view the following page: http://localhost/cgi-bin/hello.py

GET and POST Methods

When we send a request to the web server, sometimes we want to send some data over. Browser use two

methods to pass data to web server: GET and POST.

GET method

http://localhost/cgi-bin/hello.py?key1=value1&key2=value2&key3=value3

Data in the GET method appears in your URL

Never use GET method to pass sensitive information.

Size limitation: only 1024 characters can be sent in a request string.

Bellow is get.py script. Put the file under xampp\cgi-bin folder. Then enter the following URL in a browser as

follows. Observe how data is sent using get method.

http://localhost/cgi-bin/get.py?firstName=Mary&lastName=Smith

#!C:\Users\UIC\AppData\Local\Programs\Python\Python37-32\python.exe

# you should replace the first comment according where 'python.exe' is in your own

computer.

print("Content-type: text/html\r\n\r\n")

print("<html>")

print("<head>")

print("<title>Hello World - First CGI Program</title>")

print("</head>")

print("<body>")

print("<h2>Hello World! This is my first CGI program!</h2>")

print("</body>")

print("</html>")

#!C:\Users\UIC\AppData\Local\Programs\Python\python37-32\python.exe

# Import modules for CGI handling

import cgi

import cgitb

# Create instance of FieldStorage

form = cgi.FieldStorage()

# Get data from fields

firstName = form.getvalue('firstName')

lastName = form.getvalue('lastName')

print("Content-type:text/html\r\n\r\n")

print("<html>")

print("<head>")

print("<title>Get Name</title>")

print("</head>")

print("<body>")

Here is an example to show you how to send and retrieve

data in a post method.

C:\xampp\htdoc\majorSelectionForm.html

print("<h2>Hello %s %s</h2>" % (firstName, lastName))

print("</body>")

print("</html>")

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Major Selection</title>

</head>

<body>

<h1>Major Selection</h1>

<form action="../cgi-bin/displayInfo.py" method="post">

<table>

<tr>

<td>

Student ID: <input type="text" name="ID"/><br/>

Student Name: <input type="text" name="name"/>

</td>

</tr>

<tr>

<td>

Major:

<select name="major">

<option value="CST" selected>CST</option>

<option value="DS">DS</option>

</select>

</td>

</tr>

<tr>

<td>

<input type="radio" name="bestlang" value="C++" />C++

<input type="radio" name="bestlang" value="Java" />Java

<input type="radio" name="bestlang" value="Python" />Python

</td>

</tr>

<tr>

<td>

<input type="submit" value="submit">

</td>

</tr>

</table>

</form>

</body>

</html>

C:\xampp\cgi-bin\displayInfo.py

#!C:\Users\UIC\AppData\Local\Programs\Python\Python37-32\python.exe

import cgi

import cgitb

form = cgi.FieldStorage()

# Fetch data edit boxes

studentID = form.getvalue('ID')

studentName = form.getvalue('name')

# drop down box

if form.getvalue('major'):

major = form.getvalue('major')

else:

major = "No Selection"

# checkbox

if form.getvalue('C++'):

CPlusFlag = "C++"

else:

CPlusFlag = ""

if form.getvalue('Java'):

JavaFlag = "Java"

else:

JavaFlag = ""

if form.getvalue('Python'):

PythonFlag = "Python"

else:

PythonFlag = ""

# radio button

if form.getvalue('bestlang'): # radio

bestlang = form.getvalue('bestlang')

else:

bestlang = ""

if form.getvalue('textcontent'):

textContent = form.getvalue('textcontent')

else:

textContent = ""

print("Content-type:text/html")

print()

print("<html>")

print("<head>")

print("<meta charset=\"utf-8\">")

print("<title>Major Selection</title>")

print("</head>")

To view your page, enter address:

http://localhost/majorSelectionForm.html

Task

Suppose your company is hiring new people. Candidates will fill the following form to submit. Use CGI Python

programming to show what the user entered. Zip and submit your file on iSpace.

print("<body>")

print("<h2>%s %s</h2>" % (studentID, studentName))

print("<h2> You have picked: %s</h2>" % major)

print("<h2>You have learned: %s %s %s</h2>" %

(CPlusFlag, JavaFlag, PythonFlag))

print("<h2>You like %s the best.</h2>" % bestlang)

print("<h2>%s</h2>" % textContent)

print("</body>")

print("</html>")


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp