Download - Python lecture 11
![Page 1: Python lecture 11](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/1.jpg)
CS 3430: Introduction to Python and Perl
Lecture 11
Department of Computer ScienceUtah State University
![Page 2: Python lecture 11](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/2.jpg)
Outline● Basic Network Programming● Introduction to PIL
![Page 3: Python lecture 11](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/3.jpg)
Basic Network Programming
![Page 4: Python lecture 11](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/4.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/5.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/6.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/7.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/8.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/9.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/10.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/11.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/12.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/13.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/14.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/15.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/16.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/17.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/18.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/19.jpg)
Minimal Forking Server
minimal_forking_server.py
![Page 20: Python lecture 11](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/20.jpg)
Minimal Threading Server
minimal_threading_server.py
![Page 21: Python lecture 11](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/21.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/22.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/23.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/24.jpg)
Getting Ready to Take Your PIL
![Page 25: Python lecture 11](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/25.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/26.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/27.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/28.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022020105/55632abbd8b42a57348b4f10/html5/thumbnails/29.jpg)
Reading
Ch. 14 in Beginning Python by Hetland.