Composing Email

To compose an email message, you can either create an empty instance of a Message object, which is defined in the email.message module, or you can use a Message object that was created by parsing an email message (see the previous section). Creates a new message that is initially empty. An instance m of Message supports the following methods for populating a message with content, headers, and other information. Adds a new message header. name is the name of the header, value is the value of the...

DBMStyle Database Modules

Python includes a number of library modules for supporting UNIX DBM-style database files. Several standard types of these databases are supported. The dbm module is used to read standard UNIX-dbm database files. The gdbm module is used to read GNU dbm database files (http www.gnu.org software gdbm).The dbhash module is used to read database files created by the Berkeley DB library (http www.oracle.com database berkeley-db index.html).The dumbdbm module is a pure-Python module that implements a...

Network Programming Basics

Python's network programming modules primarily support two Internet protocols TCP and UDP. The TCP protocol is a reliable connection-oriented protocol used to establish a two-way communications stream between machines. UDP is a lower-level packet-based protocol (connectionless) in which machines send and receive discrete packets of information without formally establishing a connection. Unlike TCP, UDP communication is unreliable and thus inherently more complicated to manage in applications...

Parsing Email

At the top level, the email module provides two functions for parsing messages message_from_file(f) Parses an email message read from the file-like object f which must be opened in text mode. The input message should be a complete MIME-encoded email message including all headers, text, and attachments. Returns a Message instance. Parses an email message by reading an email message from the text string str. Returns a Message instance. A Message instance m returned by the previous functions...

String Literals

String literals are used to specify a sequence of characters and are defined by enclosing text in single ('), double (), or triple (''' or ) quotes.There is no semantic difference between quoting styles other than the requirement that you use the same type of quote to start and terminate a string. Single- and double-quoted strings must be defined on a single line, whereas triple-quoted strings can span multiple lines and include all of the enclosed formatting (that is, newlines, tabs, spaces,...

Restkey Csv Python

The csv module is used to read and write files consisting of comma-separated values (CSV). A CSV file consists of rows of text, each row consisting of values separated by a delimiter character, typically a comma (,) or a tab. Here's an example Blues,Elwood,1060 W Addison,Chicago, IL Variants of this format commonly occur when working with databases and spreadsheets. For instance, a database might export tables in CSV format, allowing the tables to be read by other programs. Subtle complexities...

Python Json Reference

The json module is used to serialize and unserialize objects represented using JavaScript Object Notation (JSON). More information about JSON is available at http json.org, but the format is really just a subset of JavaScript syntax. Incidentally, it's almost the same as Python syntax for representing lists and dictionaries. For example, a JSON array is written as value1, value2, , and a JSON object is written as name-.value, name-.value, . The following list shows how JSON values and Python...

Urllib Package

The urllib package provides a high-level interface for writing clients that need to interact with HTTP servers, FTP servers, and local files. Typical applications include scraping data from web pages, automation, proxies, web crawlers, and so forth. This is one of the most highly configurable library modules, so every last detail is not presented here. Instead, the most common uses of the package are described. In Python 2, the urllib functionality is spread across several different library...

XML Example Document

The following example illustrates a typical XML document, in this case a description of a recipe. lt xml version 1.0 encoding iso-8859-1 gt lt recipe gt lt title gt lt item num 4 gt Large avocados, chopped lt item gt lt item num 1 gt Tomato, chopped lt item gt lt item num 1 2 units C gt White onion, chopped lt item gt lt item num 2 units tbl gt Fresh squeezed lemon juice lt item gt lt item num 1 gt Jalapeno pepper, diced lt item gt lt item num 1 units tbl gt Fresh cilantro, minced lt item gt lt...

Http Package

The http package consists of modules for writing HTTP clients and servers as well as support for state management cookies .The Hypertext Transfer Protocol HTTP is a simple text-based protocol that works as follows 1. A client makes a connection to an HTTP server and sends a request header of the following form GET document.html HTTP 1.0 Connection Keep-Alive User-Agent Mozilla 4.61 en X11 U SunOS 5.6 sun4u Host rustler.cs.uchicago.edu 8000 Accept image gif, image x-xbitmap, image jpeg, image...

Semaphore and Bounded Semaphore

A semaphore is a synchronization primitive based on a counter that's decremented by each acquire call and incremented by each release call. If the counter ever reaches zero, the acquire method blocks until some other thread calls release . Creates a new semaphore. value is the initial value for the counter. If omitted, the counter is set to a value of 1. A Semaphore instance, s, supports the following methods Acquires the semaphore. If the internal counter is larger than zero on entry, this...

Predefined Exception Classes

The following exceptions are raised by programs Failed attribute reference or assignment. EOFError End of file. Generated by the built-in functions input and raw_input . It should be noted that most other I O operations such as the read and readline methods of files return an empty string to signal EOF instead of raising an exception. Failed floating-point operation. It should be noted that floating-point exception-handling is a tricky problem and only that this exception only gets raised if...

Managed Objects

Unlike threads, processes do not support shared objects. Although you can create shared values and arrays as shown in the previous section, this doesn't work for more advanced Python objects such as dictionaries, lists, or instances of user-defined classes.The multiprocessing module does, however, provide a way to work with shared objects if they run under the control of a so-called manager.A manager is a separate subprocess where the real objects exist and which operates as a server. Other...

Error Handling

Extension modules indicate errors by returning NULL to the interpreter. Prior to returning NULL, an exception should be set using one of the following functions Raises a MemoryError exception. void PyErr_SetFromErrno PyObject exc Raises an exception. exc is an exception object.The value of the exception is taken from the errno variable in the C library. void PyErr_SetFromErrnoWithFilename PyObject exc, char filename Like PyErr_SetFromErrno , but includes the file name in the exception value as...