Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 15 ■ PYTHON AND THE WEB 325 Figure 15-1. A CGI traceback from the cgitb module Using the cgi Module So far the programs have only produced output; they haven’t used any form of input. Input is supplied to the CGI script from an HTML form (described in the next section) as key-value pairs, or fields. You can retrieve these fields in your CGI script using the FieldStorage class from the cgi module. When you create your FieldStorage instance (you should create only one), it fetches the input variables (or fields) from the request and presents them to your program through a dictionary-like interface. The values of the FieldStorage can be accessed through ordinary key lookup, but due to some technicalities (related to file uploads, which we won’t be dealing with here) the elements of the FieldStorage aren’t really the values you’re after. For example, if you know the request contained a value named name, you couldn’t simply do this: form = cgi.FieldStorage() name = form['name'] You’d have to do this: form = cgi.FieldStorage() name = form['name'].value
326 CHAPTER 15 ■ PYTHON AND THE WEB A simpler way of fetching the values is the getvalue method, which is similar to the dictionary method get, except that it returns the value of the value attribute of the item. For example: form = cgi.FieldStorage() name = form.getvalue('name', 'Unknown') In the preceding example, I have supplied a default value ('Unknown'). If you don’t supply one, None will be the default. The default is used if the field is not filled in. Listing 15-6 contains a simple example that uses cgi.FieldStorage. Listing 15-6. A CGI Script That Retrieves a Single Value from a FieldStorage (simple2.cgi) #!/usr/bin/env python import cgi form = cgi.FieldStorage() name = form.getvalue('name', 'world') print 'Content-type: text/plain' print print 'Hello, %s!' % name INVOKING CGI SCRIPTS WITHOUT FORMS Input to CGI scripts generally comes from Web forms that have been submitted, but it is also possible to call the CGI program with parameters directly. You do this by adding a question mark after the URL to your script, and then adding key-value pairs separated by ampersands (&). For example, if the URL to the script in Listing 15-6 were http://www.someserver.com/simple2.cgi, you could call it with name=Gumby and age=42 with the URL http://www.someserver.com/simple2.cgi?name=Gumby&age=42. If you try that, you should get the message “Hello, Gumby!” instead of “Hello, world!” from your CGI script. (Note that the age parameter isn’t used.) You can use the urlencode method of the urllib module to create this kind of URL query: >>> urllib.urlencode({'name': 'Gumby', 'age': '42'}) 'age=42&name=Gumby' You can, of course, use this strategy in your own programs, together with urllib, to create a screen scraping program that can actually interact with a CGI script. However, if you’re writing both ends (that is, both server and client side) of such a contraption, you would, most likely, be better off using some form of Web service (as described later in this chapter). Note that there are two main ways of getting information from a CGI script: the GET method and the POST method. For the purposes of this chapter, the difference between the two isn’t really important (basically, GET is for retrieving things, and encodes its query in the URL, while POST can be used for any kind of query, but encodes the query a bit differently). For more information about GET and POST, see the forms tutorials in the next section.
- Page 305 and 306: 274 CHAPTER 12 ■ GRAPHICAL USER I
- Page 307 and 308: 276 CHAPTER 12 ■ GRAPHICAL USER I
- Page 309 and 310: 278 CHAPTER 12 ■ GRAPHICAL USER I
- Page 311 and 312: 280 CHAPTER 12 ■ GRAPHICAL USER I
- Page 313 and 314: 282 CHAPTER 12 ■ GRAPHICAL USER I
- Page 316 and 317: CHAPTER 13 ■ ■ ■ Database Sup
- Page 318 and 319: CHAPTER 13 ■ DATABASE SUPPORT 287
- Page 320 and 321: CHAPTER 13 ■ DATABASE SUPPORT 289
- Page 322 and 323: CHAPTER 13 ■ DATABASE SUPPORT 291
- Page 324 and 325: CHAPTER 13 ■ DATABASE SUPPORT 293
- Page 326: CHAPTER 13 ■ DATABASE SUPPORT 295
- Page 329 and 330: 298 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 331 and 332: 300 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 333 and 334: 302 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 335 and 336: 304 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 337 and 338: 306 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 339 and 340: 308 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 341 and 342: 310 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 343 and 344: 312 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 345 and 346: 314 CHAPTER 15 ■ PYTHON AND THE W
- Page 347 and 348: 316 CHAPTER 15 ■ PYTHON AND THE W
- Page 349 and 350: 318 CHAPTER 15 ■ PYTHON AND THE W
- Page 351 and 352: 320 CHAPTER 15 ■ PYTHON AND THE W
- Page 353 and 354: 322 CHAPTER 15 ■ PYTHON AND THE W
- Page 355: 324 CHAPTER 15 ■ PYTHON AND THE W
- Page 359 and 360: 328 CHAPTER 15 ■ PYTHON AND THE W
- Page 361 and 362: 330 CHAPTER 15 ■ PYTHON AND THE W
- Page 363 and 364: 332 CHAPTER 15 ■ PYTHON AND THE W
- Page 365 and 366: 334 CHAPTER 15 ■ PYTHON AND THE W
- Page 367 and 368: 336 CHAPTER 15 ■ PYTHON AND THE W
- Page 369 and 370: 338 CHAPTER 15 ■ PYTHON AND THE W
- Page 372 and 373: CHAPTER 16 ■ ■ ■ Testing, 1-2
- Page 374 and 375: CHAPTER 16 ■ TESTING, 1-2-3 343 W
- Page 376 and 377: CHAPTER 16 ■ TESTING, 1-2-3 345 d
- Page 378 and 379: CHAPTER 16 ■ TESTING, 1-2-3 347 u
- Page 380 and 381: CHAPTER 16 ■ TESTING, 1-2-3 349 F
- Page 382 and 383: CHAPTER 16 ■ TESTING, 1-2-3 351 P
- Page 384 and 385: CHAPTER 16 ■ TESTING, 1-2-3 353 "
- Page 386: CHAPTER 16 ■ TESTING, 1-2-3 355 N
- Page 389 and 390: 358 CHAPTER 17 ■ EXTENDING PYTHON
- Page 391 and 392: 360 CHAPTER 17 ■ EXTENDING PYTHON
- Page 393 and 394: 362 CHAPTER 17 ■ EXTENDING PYTHON
- Page 395 and 396: 364 CHAPTER 17 ■ EXTENDING PYTHON
- Page 397 and 398: 366 CHAPTER 17 ■ EXTENDING PYTHON
- Page 399 and 400: 368 CHAPTER 17 ■ EXTENDING PYTHON
- Page 401 and 402: 370 CHAPTER 17 ■ EXTENDING PYTHON
- Page 404 and 405: CHAPTER 18 ■ ■ ■ Packaging Yo
326 CHAPTER 15 ■ PYTHON AND THE WEB<br />
A simpler way of fetching the values is the getvalue method, which is similar <strong>to</strong> the dictionary<br />
method get, except that it returns the value of the value attribute of the item. For example:<br />
form = cgi.FieldS<strong>to</strong>rage()<br />
name = form.getvalue('name', 'Unknown')<br />
In the preceding example, I have supplied a default value ('Unknown'). If you don’t supply<br />
one, None will be the default. The default is used if the field is not filled in.<br />
Listing 15-6 contains a simple example that uses cgi.FieldS<strong>to</strong>rage.<br />
Listing 15-6. A CGI Script That Retrieves a Single Value from a FieldS<strong>to</strong>rage (simple2.cgi)<br />
#!/usr/bin/env python<br />
import cgi<br />
form = cgi.FieldS<strong>to</strong>rage()<br />
name = form.getvalue('name', 'world')<br />
print 'Content-type: text/plain'<br />
print<br />
print 'Hello, %s!' % name<br />
INVOKING CGI SCRIPTS WITHOUT FORMS<br />
Input <strong>to</strong> CGI scripts generally comes from Web forms that have been submitted, but it is also possible <strong>to</strong> call<br />
the CGI program with parameters directly. You do this by adding a question mark after the URL <strong>to</strong> your script,<br />
and then adding key-value pairs separated by ampersands (&). For example, if the URL <strong>to</strong> the script in<br />
Listing 15-6 were http://www.someserver.com/simple2.cgi, you could call it with name=Gumby and<br />
age=42 with the URL http://www.someserver.com/simple2.cgi?name=Gumby&age=42. If you try<br />
that, you should get the message “Hello, Gumby!” instead of “Hello, world!” from your CGI script. (Note that<br />
the age parameter isn’t used.) You can use the urlencode method of the urllib module <strong>to</strong> create this kind<br />
of URL query:<br />
>>> urllib.urlencode({'name': 'Gumby', 'age': '42'})<br />
'age=42&name=Gumby'<br />
You can, of course, use this strategy in your own programs, <strong>to</strong>gether with urllib, <strong>to</strong> create a screen<br />
scraping program that can actually interact with a CGI script. However, if you’re writing both ends (that is, both<br />
server and client side) of such a contraption, you would, most likely, be better off using some form of Web<br />
service (as described later in this chapter).<br />
Note that there are two main ways of getting information from a CGI script: the GET method and the POST<br />
method. For the purposes of this chapter, the difference between the two isn’t really important (basically, GET is for<br />
retrieving things, and encodes its query in the URL, while POST can be used for any kind of query, but encodes<br />
the query a bit differently). For more information about GET and POST, see the forms tu<strong>to</strong>rials in the next<br />
section.