加载中...

Python

  1. Home
  2. Computing & Technology
  3. Python

Part 2: Getting Feed Information From the Data File With Python

By Al Lukaszewski, About.com

5 of 9

Holding the User Data in Python With PHP

As you will note in the PHP script, the program is called as a shell command with the user input as an argument. Therefore, we can retain the 'py' suffix and receive the user's input with the arv variable of the sys module. The code looks like this:

feedname = sys.argv[1]

If you are in any doubt about how to use the sys module or the processing of arguments from the command line, you can read a brief discussion of how to do it in [link url=http://python.about.com/od/pythonanddatabases/ss/mysql_insert_4.htm] my discussion on working with MySQL databases [/link].

One aspect of the PHP version that is very nice for debugging is that the program is executed as a shell command. This modularises both development and troubleshooting, allowing one to feed options to the Python program separate of the PHP script. For example, using the sys module, one could call the program as follows:

./RSSReader.py "Spiegel"
The output will be the same as that sent to the web page.

If, however, you do not use CGI or otherwise want to hardwire the feedname assignment into the program, just insert after either version and the variable will be over-written:

feedname = "Guardian World"

Next, we will define the function to read the data file.

  1. This is NOT the Beginning
  2. Python Best Practice: the Bang Line
  3. The Tasks This Python Program Must Do
  4. Holding the User Data in Python With CGI
  5. Holding the User Data in Python With PHP
  6. Defining the First Python Function
  7. Reading the RSS Feed as a Python File Object
  8. Using a Python Iterator
  9. Calling Python's Function, Retrieving the Feed Address

<< Previous | Next >>

5 of 9

Index: Part 2: Getting Feed Information From the Data File With Python

    Explore Python

    More from About.com

    Python

    1. Home
    2. Computing & Technology
    3. Python

    ©2009 About.com, a part of The New York Times Company.

    All rights reserved.