Python
  Home arrow Python arrow Page 2 - Python on the Web
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Python on the Web
By: Mark Lee Smith
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 27
    2004-05-18

    Table of Contents:
  • Python on the Web
  • Creating a Warning
  • Cookies
  • Sending Email
  • A Last Example
  • Inside uploads()...

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Dell PowerEdge Servers

    Python on the Web - Creating a Warning
    (Page 2 of 6 )

    Like life itself, we seem to want more control. And this next function we'll create will give us just that -- and output a warning if the field wasn't filled in correctly!

    #!/usr/bin/env python

    import cgi, re

    form = cgi.FieldStorage()

    def check(**fields):
     for field in fields:
      if field in form:
       value = form[field].value
       if re.search(fields[field], value): print value + '<br />'
       else: print field, 'was not filled in correctly!<br />'

    if __name__ == '__main__':
     
     print 'Content-Type: text/htmln'
     
     if 'submit' and 'done' in form:
      check(name = '^[a-zA-Z ]+$', age = '^d{2}$')
     else:
      print 'if you were directed here in error please visit here.com'

    Since this is pretty similar to our other function, I'm just going to skip over it quickly. If you've ever used Perl, then you've spotted the regular expressions hiding in there.

    Regular expressions in Python are accessed though the 're' module (Python's regular expression library), for obvious reasons... regular expressions still seem like the best way to describe and check form values.

    If you want to find out more about regular expressions and Python check out the 're' module at http://www.python.org/doc/2.3.3/lib/module-re.html

    Then if the value isn't what we want, we get a warning instead of just ignoring the field.

    Anyone wondering what **fields is all about? I'm sure someone is. The simplest explanation I can think of is that it tells the function to expect a variable number of arguments in X format (currying in functional programming) i.e.:

    >>> def show(*args, **kwds):
    ...     print args, kwds
    ...
    >>> show()
    () {}
    >>> show('interesting', 'dont ya think!', arg1 = 'str1', arg2 = 2)
    ('interesting', 'dont ya think!') {'arg1': 'str1', 'arg2': 2}
    >>>

    Simple, and very useful form time to time! Time for a cookie break... No, not the food... although, still pretty sweet over all.

    More Python Articles
    More By Mark Lee Smith


     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway