Python lecture 11

29
CS 3430: Introduction to Python and Perl Lecture 11 Department of Computer Science Utah State University

Transcript of Python lecture 11

Page 1: Python lecture 11

CS 3430: Introduction to Python and Perl

Lecture 11

Department of Computer ScienceUtah State University

Page 2: Python lecture 11

Outline● Basic Network Programming● Introduction to PIL

Page 3: Python lecture 11

Basic Network Programming

Page 4: Python lecture 11

Sockets

● Sockets are the most widely used form of Inter Process Communication (IPC).

● Sockets are used for cross-platform communication.

● Sockets were invented in Berkeley as part of the BSD flavor of UNIX.

● Sockets are now available on all major platforms.

Page 5: Python lecture 11

Sockets

● Sockets are a basic component of network programming.

● A socket can be thought of as an information channel with a program on both ends.

● The module that implements sockets is called socket.

● There are two types of sockets: client sockets and server sockets.

Page 6: Python lecture 11

Clients and Servers

● A client is a program that requests information from a server.

● A server is a program that provides information to clients.

● Client programs use client sockets.● Server programs use server sockets.

Page 7: Python lecture 11

Server Sockets● A server socket object must be bound to a host

(www.usu.edu) and a port (an integer, e.g. 1234) on that host.

● The socket object specifies how many connecting requests can be queued up before the connecting requests are rejected.

● After that the server program goes into an infinite loop that works as follows:

Accept a connection Do something with that connection

Page 8: Python lecture 11

Client Sockets● A client socket object connects to a host and a

port.● The client socket object must then send and/or

receive information from the socket object.● Blocking (synchronous) servers process one

client connection at a time.● Non-blocking (asynchronous) servers process

multiple clients at a time.

Page 9: Python lecture 11

Transmission Control Protocol● TCP stands for Transmission Control Protocol.● TCP is one of the two original core protocols of

the Internet Protocol Suite.● TCP/IP is a frequent abbreviation used in the

literature● It is connection oriented.

Page 10: Python lecture 11

User Datagram Protocol● UDP stands for User Datagram Protocol.● UDP is another member of the original Internet

Protocol Suite.● Under UDP, processes can send messages

(called datagrams) to each other without prior special communications that set up channels or data paths

● It is connectioless.

Page 11: Python lecture 11

Minimal Server and Minimal Client

● Write a blocking server that sets a server socket on the local computer on port 1234 and goes into an infinite loop that accepts client connections, gets the address of each connecting client, and sends the string 'Thank you for connecting' to each client.

● Write a client that connects to a given host on a given port and prints 1024 bytes of information that it receives from the server.

● Source code: minimal_server.py and minimal_client.py

Page 12: Python lecture 11

Module SocketServer

● The SocketServer module is used for more advanced server programming.

● SocketServer contains for classes: TCPServer, UDPServer, UnixStreamServer, UnixDatagramServer.

● TCPServer is used to program TCP socket streams.

● UDPServer for UDP datagram sockets.

Page 13: Python lecture 11

Module SocketServer

● The SocketServer framework provides request handlers.

● Each time a server gets a request (a connection from a client), a request handler object is constructed and its methods are called to handle the request.

● The BasicRequestHandler class places all of its action in the handle method.

Page 14: Python lecture 11

TCPServer and StreamRequestHandler

● The TCPServer class allows you to create TCP servers.

● To handle requests on a TCPServer, you can use the StreamRequestHandler class.

● StreamRequestHandler objects have two attributes: self.rfile (stream for reading) and self.wfile (stream for writing).

● These file-like objects are used to communicate with clients.

Page 15: Python lecture 11

Minimal TCP Server● Write a blocking TCP server that sets a server socket on

the local computer on port 1234 and goes into an infinite loop that processes client connections with custom StreamRequestHandler objects.

● A custom stream request handler inherits from StreamRequestHandler and overrides the handle method to print the address of the connection client and send 'Thank you for connecting' to the client.

Page 16: Python lecture 11

Handling Multiple Connections● There are three ways of handling multiple connections:

forking, threading, and asynchronous I/O.● Forking is a UNIX term that loosely means duplication.● When a parent process (running program) forks a child

process, both processes start running in parallel with their own separate memories.

● In a forking server, a child process is forked for every connection while the parent process (server) keeps listening for new connections.

Page 17: Python lecture 11

Handling Multiple Connections● Forking is resource-intensive, because each forked

process requires its own memory.● Threading is an alternative to forking.● A thread is a lightweight process that exists within the

same parent process and shares the same memory.● The upside is efficiency.● The downside is the necessity of thread

synchronization, because threads share the same memory.

Page 18: Python lecture 11

Handling Multiple Connections● Some operating systems (Windows) do not support

forking.● If your system supports forking and you do not want to

bother with synchronization issues, forking is a reasonable option.

Page 19: Python lecture 11

Minimal Forking Server

minimal_forking_server.py

Page 20: Python lecture 11

Minimal Threading Server

minimal_threading_server.py

Page 21: Python lecture 11

URL Access● Python has two modules for processing URLs: urllib

and urllib2.

● The urllib module is more basic and more straightforward.

● The urllib2 module is more advanced (and less straightforward): handles HTTP authentication, cookies, protocol extensions, etc.

Page 22: Python lecture 11

Opening Remote and Local Files● The urllib object has the function urlopen that allows

you to open remote and local files.● The function returns a file-like object that supports the close, read, readline, and readlines methods and iteration.

● Once you have a file-like object to a remote webpage, you can use regular expressions to extract specific information from the text of that webpage.

Page 23: Python lecture 11

Getting HTML Source● The module urllib enables you to retrieve the html

source of a URL.● The retrieved html source can be parsed for links or

indexed in a local database.● html_source.py prints the html source of a URL.and

saves it in a local file.

Page 24: Python lecture 11

Getting Ready to Take Your PIL

Page 25: Python lecture 11

Installation

● PIL is available at www.pythonware.org

● There are PIL versions for various Python releases 2.7, 2.6, etc.

● We will use PIL 1.1.7 for Python 2.7

Page 26: Python lecture 11

PIL on 64bit Win

● There may still be issues with 64bit versions of PIL on Windows● Here is a forum that suggest links to 64bit versions of PIL as well

as other possible workarounds on 64bit version of Python http://stackoverflow.com/questions/2088304/installing-pil-

python-imaging-library-in-win7-64-bits-python-2-6-4● The simplest way to handle 64bit issues is to:

Uninstall any previously installed PIL version Install Python 32bit Version for Windows (Python 2.7) Install PIL 32 bit Version (for Python 2.7)

Page 27: Python lecture 11

PIL on Mac

● In order to install PIL on a Mac it needs to be built from the source

● Source is at http://www.pythonware.com/products/pil/● Inside tar.gz there is README that describes which

commands need to be ran to install PIL● There are dependencies for the build as well● These dependencies are also described in README

Page 28: Python lecture 11

Checking PIL Availability

● Run this program interactively in the interpreter or or as a program:

import Image

im = Image.new(“RGB”, (32, 32))

print im● If this works, PIL is installed

Page 29: Python lecture 11

Reading

Ch. 14 in Beginning Python by Hetland.