REST Quick Start Finding Bargains on Amazoncom
Amazon.com, the popular online store, makes much of its data available through a REST web service called Amazon Web Services. Perhaps the most interesting feature of this web service is the capability it offers to search for books or other items and then retrieve metadata, pictures, and reviews for an item. Amazon effectively gives you programmatic access to its product database, something that would be difficult to duplicate or obtain by other means. The Amazon Web Services homepage is at http...
Embedding the Jython Interpreter
By embedding the Jython interpreter in your own Java classes, you can run scripts from within your application, gaining control over the complete environment. That's important because few Java applications run from the command line. You can find the Jython interpreter in the class org.python.util.PythonInterpreter. You can use code like the following to initialize the Jython interpreter Properties props new Properties props.put python.home, pythonHome PythonInterpreter.initialize...
The SOAP Response
Here's a possible response you might get from a SOAP server after sending it the sortList request lt xml version 1.1 encoding UTF-8 gt lt SOAP-ENV Envelope encoding lt SOAP-ENV Body lt lt ns1 sortList xmlns ns1 urn SearchSort lt lt tem gt 10 lt item gt lt return lt lt lt ns1 sortList gt lt lt SOAP-ENV Envelope gt Just as with XML-RPC, a SOAP response has the same basic structure as a SOAP request. Where the SOAP request had a list of arguments, the SOAP response has a single return value. This,...
Sending Mail with SMTP and smtplib
Now that you know how to construct e-mail messages, it's appropriate to revisit in a little more detail the protocol used to send them. This is SMTP, another TCP IP-based protocol, defined in RFC 2821. Look at the original example one more time gt gt gt fromAddress 'sender example.com' gt gt gt toAddress your e-mail address gt gt gt msg Subject Hello n nThis is the body of the message. gt gt gt import smtplib gt gt gt server smtplib.SMTP localhost, 25 gt gt gt server.sendmail fromAddress,...
Putting It All Together with Extreme Programming
A good way to see how all of this fits together is to use a test suite during the development of an extended coding project. This strategy underlies the XP Extreme Programming methodology, which is a popular trend in programming First, you plan the code then you write the test cases as a framework and only then do you write the actual code. Whenever you finish a coding task, you rerun the test suite to see how closely you approach the design goals as embodied in the test suite. Of course, you...
Using Java Classes in Jython
In general, treat Java classes as Python classes in your scripts. Jython uses the Python syntax for importing Java classes. Just think of Java packages as a combination of Python modules and classes. For example, to import java.util.Map into a Jython script, use the following code Note how this looks just like a Python import. You can try this out in your own scripts, as shown in the following example. Enter the following script and name the file jystring.py import sys from java.lang import...
Summary Orf
The key things to take away from this chapter are How to parse XML using both SAX and DOM How to validate XML using xmlproc How to parse HTML using HTMLParser In Chapter 16, you learn more about network programming and e-mail. Before proceeding, however, try the exercises that follow to test your understanding of the material covered in this chapter. You can find the solutions to these exercises in Appendix A.
Accessing Databases from Jython
JDBC, or Java Database Connectivity, provides a set of APIs to access databases in a consistent manner. Most, but not all, differences between databases can be ignored when working with JDBC. Python has a set of database APIs as well, as described in Chapter 14. A large difference between the Python APIs and the Java APIs is that the Java JDBC drivers are almost all written entirely in Java. Furthermore, almost all JDBC drivers are written by the database vendors. Most Python DB drivers, such...
The LAME Extension Module
To create an extension module that enables you to encode a raw audio file into an MP3 could be as simple as creating a simple function that invokes the encode function you defined in the preceding example defined in clame.c int encode char , char static PyObject pylame1_encode PyObject self, PyObject args int status char inpath char o utpath if PyArg_ParseTuple args, ss, npath, amp outpath return NULL status encode inpath, outpath return Py_BuildValue i, status static PyMethodDef...
Integrating Java and Jython
The advantage of Jython comes into play when you integrate the Jython interpreter into your Java applications. With this combination, you can get the best of both the scripting world and the rich set of Java APIs. Jython enables you to instantiate objects from Java classes and treat these objects as Python objects. You can even extend Java classes within Jython scripts. Jython actively tries to map Java data types to Python types and vice versa. This mapping isn't always complete because the...
Integrating Java with Python
Java is an object-oriented programming language. Java programs are compiled from source code into byte codes. The Java runtime engine, called a Java virtual machine, or JVM, runs the compiled byte codes. Sound familiar At an abstract level at least, Java and Python are very similar. Like Java, Python programs are compiled into byte codes, although this can be done at runtime. Despite these similarities, some differences between the languages exist With Python, you can run scripts directly from...
Parsing a Local Mail Spool with mailbox
If you have a UNIX shell account on your mail server because, for instance, you run a mail server on your own computer , mail for you is appended to a file probably var spool mail your username as it comes in. If this is how your mail setup works, your existing mail client is probably set up to parse that file. It may also be set up to move messages out of the spool file and into your home directory as they come in. The incoming mailbox in var spool mail is kept in a particular format called...
Chapter Mol
1. Suppose you need to write a Python script to store the pizza preferences for the workers in your department. You need to store each person's name along with that person's favorite pizza toppings. Which technologies are most appropriate to implement this script a. Set up a relational database such as MySQL or Sqlite. b. Use a dbm module such as dbm. C. Implement a web-service-backed rich web application to create a buzzword-compliant application. 2. Rewrite the following example query using...
Writing Java EE Servlets in Jython
Most Java development revolves around enterprise applications. To help or hinder, depending on your view , Java defines a set of standards called Java EE, or Java Platform Enterprise Edition. The Java EE standards define an application server and the APIs such a server must support. Organizations can then choose application servers from different vendors, such as WebSphere from IBM, WebLogic from Bea, JBoss from the JBoss Group, and Tomcat from the Apache Jakarta project. Java developers write...
Design of the Python Chat Server
In IRC, a client that connects to a server must provide a nickname a short string identifying the person who wants to chat. A nickname must be unique across a server so that users can't impersonate one another. Your server will carry on this tradition. An IRC server provides an unlimited number of named channels, or rooms, and each user can join any number of rooms. Your server will provide only a single, unnamed room, which all connected users will inhabit. Entering a line of text in an IRC...
HumanReadable API Documentation
In my opinion, no matter which web service protocol you're using, nothing beats an up- to - date human -readable description of an API. This can be written manually or generated through introspection and the use of Python docstrings. Up next are three sample documents that describe the three web service APIs for the BittyWiki application created in this chapter. They're all extremely short, but they contain all the information a user needs to write an application using any of them. To get the...
Chapter Gkz
1. Write a function called do_plus that accepts two parameters and adds them together with the operation. 2. Add type checking to confirm that the type of the parameters is either an integer or a string. If the parameters aren't good, raise a TypeError. 3. This one is a lot of work, so feel free to take it in pieces. In Chapter 4, a loop was written to make an omelet. It did everything from looking up ingredients to removing them from the fridge and making the omelet. Using this loop as a...
Exercise Solution Cho
if type param type and type param type 1 raise TypeError This function needs a string or an integer return first second Part 1 - fridge has to go before the omelet_type. omelet_type is an optional parameter with a default parameter, so it has to go at the end. This can be used with a fridge such as f 'eggs' 12, 'mozzarella cheese' 6, 'milk' 20, 'roast red pepper' 4, 'mushrooms' 3 or other ingredients, as you like. def make_omelet_q3 fridge, omelet_type mozzarella This will make an omelet. You...
How It Works Vhl
The number on the left is compared to the number on the right. You can compare letters, too. A few conditions exist where this might not do what you expect, such as trying to compare letters to numbers. The question just doesn't come up in many cases, so what you expect and what Python expects is probably not the same. The values of the letters in the alphabet run roughly this way A capital A is the lowest letter. B 11 is the next, followed by C 1 1 and so on until Z 11. This is followed by the...
MIME Multipart Messages
There's just one problem. This isn't quite the e-mail message described earlier. That message was a short piece of text Here' gt that picture I took of you. and an attached image. This message is just the image. There's no space for the text portion in the body of the message putting it there would compromise the image file. The Content-Type header of a mail message can be text plain or image jpeg it can't be both. So how do mail clients create messages with attachments In addition to...
The Python Chat Server
For the mirror server, the capability to support multiple simultaneous connections is useful but it doesn' t change what the server actually does. Each client interacts only with the server, and not even indirectly with the other clients. This model is a popular one web servers and mail servers use it, among others. There is another type of server, though, that exists to connect clients to each other. For many applications, it's not the server that's interesting it's who else is connected to...
Calling Jython Scripts from Java
After initializing the interpreter, you can execute a Jython script with a call to the execfile method. For example You need to pass the full name of the file to execute. You can see this in action with the following example. Enter the following Java program and name the file JyScriptRunner.java package jython import org.python.util.PythonInterpreter import org.python.core.PySystemState Initializes the Jython interpreter. public void initialize String pythonHome Properties props new Properties...
How It Works Ixs
The meal module follows the techniques shown in this chapter for creating a complete module, with testing, documentation, exceptions, classes, and functions. Note how the tests are about as long as the rest of the code. You'll commonly find this to be the case. After you've built a module, you can import the module into other Python scripts. For example, the following script calls on classes and functions in the meal module print 'Making a Breakfast' breakfast meal.makeBreakfast print 'Making a...
GUI Programming Toolkits for Python
There is wide support for writing GUIs with Python with many different toolkits You can find a dozen options at www.python.org moin GuiProgramming to try out. These toolkits, binary modules for Python that interface with native GUI code written in C C , all have different APIs and offer different feature sets. Only one comes with Python by default, the venerable TK GUI toolkit. It's always possible that if you're just using Windows, you'll install win32all and use the Win32 API directly. The...
Fetching Mail from a POP Server with poplib
Parsing a local mail spool didn't require going over the network, because you ran the script on the same machine that had the mail spool. There was no need to involve a network protocol, only a file format the format of UNIX mailboxes, derived mainly from RFC 2822 . However, most people don't have a UNIX shell account on their mail server or if they do, they want to read mail on their own machine instead of on the server . To fetch mail from your mail server, you need to go over a network,...
Navigating the File System with the os Module
The os module and its submodule os.path are one of the most helpful things about using Python for a lot of day-to-day tasks that you have to perform on a lot of different systems. If you often need to write scripts and programs on either Windows or UNIX that would still work on the other operating system, you know from Chapter 8 that Python takes care of much of the work of hiding the differences between how things work on Windows and UNIX. In this chapter, we're going to completely ignore a...
The LAME Project
LAME is or was an acronym that originally stood for LAME Ain't an MP3 Encoder. Whether or not it's officially considered an MP3 encoder isn't important to you, because it functions as a most excellent free and open-source library that is capable of encoding MP3s. Dozens of software projects use LAME but not many are implemented in Python, which is why you'll be using it as an example to demonstrate just how easy it is to create extension modules for Python that leverage an existing C code base,...
How It Works Cyk
The SubjectLister object or its TopBasedSubjectLister subclass connects to the POP3 server and sends a stat command to get the number of messages in the mailbox. A call to stat returns a tuple containing the number of messages in the mailbox, and the total size of the mailbox in bytes. The lister then iterates up to this number, retrieving every message or just the headers of every message as it goes. If SubjectLister is in use, the message is parsed with the e-mail module's Parser utility...
Chapter Eee
1. Modify the scan_pdf.py script to start at the root, or topmost, directory. On Windows, this should be the topmost directory of the current disk C , D , and so on . Doing this on a network share can be slow, so don't be surprised if your G drive takes a lot more time when it comes from a file server . On UNIX and Linux, this should be the topmost directory the root directory, . 2. Modify the scan_pdy.py script to match only PDF files with the text boobah in the file name. 3. Modify the...
How It Works Idx
When you started the SuperSimpleSocketServer, you bound the process to port 2000 of the localhost hostname. When that script called socket.accept, it stopped running and began to block on socket input, waiting for someone to connect to the server. When your telnet command opens up a TCP IP connection to the SuperSimpleSocketServer, the socket.accept method call returns from its wait. At last, someone has connected to the server The return values of socket.accept give the server the tools it...
S
save string pageName, string text method, 473 SAX basics of, 274-276 vs. DOM, 275-276 parsers, 276, 279 schemas XML , 268, 270-271 scope defined, 104 naming functions, 77 of objects, 96-99, 104-107 packages, 123 scripting with Java applications, 482-483 for Python, 483 subscripting, defined, 81 scripts CGI, Web servers, 419-420 executable Jython , 487-488 Jython, calling from Java, 508-510 Jython, controlling, 486-487 Jython, running, 485-486 text processing scripts, 189, 190, 191 turning into...
The Visible Web Server
Because you're already programming your own web servers, it's not difficult to write one that enables you to see your own sample HTTP request and response. Here's a script called VisibleWebServer.py. It includes a subclass of SimpleHTTPRequestHandler that does everything SimpleHTTPRequestHandler does, but that also captures the text of the HTTP request and response and prints them to standard output. When you make a request, it just prints out a little log message to the server's standard...
The Python Chat Server Protocol
Having decided on a feature set and a design, you must now define an application-specific protocol for your Python Chat Server. This protocol will be similar to SMTP, HTTP, and the IRC protocol in that it will run atop TCP IP to provide the structure for a specific type of application. However, it will be much simpler than any of those protocols. The mirror server also defined a protocol, though it was so simple it may have escaped notice. The mirror server protocol consists of three simple...
SocketServer
Sockets are very useful, but Python isn't satisfied with providing the same C-based socket interface you can get with most languages on most operating systems. Python goes one step further and provides socketserver, a module full of classes that let you write sophisticated socket-based servers with very little code. Most of the work in building a socketserver is defining a request handler class. This is a subclass of the socketserver module's BaseRequestHandler class, and the purpose of each...
Summary Ibh
In this chapter, you learned about the methods for making decisions that Python offers. Any operation that results in True or False can be used by if statements to determine whether a program will evaluate an indented block of code. You have seen for the first time the important role that indentation plays in Python programs. Even in the interactive Python shell the number of spaces in the indentation matters. You now have the knowledge to use sequence and dictionary elements in repetition...
Exercise Solution Tmw
gt gt gt fridge butter Dairy spread, peanut butter non-dairy spread, cola fizzy water gt gt gt food_sought chocolate milk gt gt gt try fridge food_sought except KeyError print s wasn't found in the fridge food_sought else print Found what I was looking for s is s food_sought, fridge food_key chocolate milk wasn't found in the fridge
Fetching Mail from an IMAP Server with imaplib
The other protocol for accessing a mailbox on a remote server is IMAP, the Internet Message Access Protocol. The most recent revision of IMAP is defined in RFC 3501, and it has significantly more features than POP3. It's also gaining in popularity over POP3. The main difference between POP3 and IMAP is that POP3 is designed to act like a mailbox It just holds your mail for a while until you collect it. IMAP is designed to keep your mail permanently stored on the server. Among other things, you...
Soap
XML - RPC solves REST's main problem by defining a standard way to represent data types such as integers, dates, and lists. However, while XML-RPC was being defined, the W3C's XML working group was working on its own representation of those data types and many others. After XML-RPC became popular, the W3C turned its attention to it, and started redesigning it to use WC3' s preexisting standards. Along the way, ambition broadened the scope of the project to include any kind of message exchange,...
MIME Encodings Quotedprintable and Base
The most important parts of MIME are its encodings, which provide ways of encoding 8-bit characters into 7 bits. MIME defines two encodings quoted-printable encoding and Base64 encoding. Python provides a module for moving strings into and out of each encoding, The quoted-printable encoding is intended for text that contains only a few 8-bit characters, with the majority of characters being U.S. ASCII. The advantage of the quoted-printable encoding is that the text remains mostly legible once...
Multithreaded Servers
One problem with both of these implementations of the mirror server is that only one client at a time can connect to a running server. If you open two telnet sessions to a running server, the second session won't finish connecting until you close the first one. If real servers worked this way, nothing would ever get done. That's why most real servers spawn threads or subprocesses to handle multiple connections. The SocketServer module defines two useful classes for handling multiple connections...
Binding to an External Hostname
If you tried to telnet into the SuperSimpleSocketServer from another machine, as suggested previously, you might have noticed that you weren't able to connect to the server. If so, it may be because you started the server by binding it to localhost. The special localhost hostname is an internal hostname, one that can't be accessed from another machine. After all, from someone else' s perspective, w ocalhost means their computer, not yours. This is actually very useful because it enables you to...
Choosing a Web Service Standard
This chapter described three standards for web services, each with a different philosophy, each with advantages and drawbacks. REST aims to get the most out of the facilities provided by HTTP, but it lacks a standard encoding for even simple data types. XML-RPC provides that encoding, but it's verbose and only deals with simple data types and compositions of simple data types. SOAP offers the structured data types of XML-RPC with the flexibility of REST, but its added complexity makes hard...
How It Works Ghv
As with the POP3 example, the first thing to do is connect to the server. POP3 servers provide only one mailbox per user, but IMAP allows one user any number of mailboxes, so the next step is to select a mailbox. The default mailbox is called Inbox, and selecting a mailbox yields the number of messages in that mailbox some POP3 servers, but not all, return the number of messages in the mailbox when you connect to the server . Unlike with POP3, IMAP lets you retrieve more than one message at...
