Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
CS 3430: Introduction to Python and Perl
Lecture 11
Department of Computer Science
Utah State University
Outline
●
●

Basic Network Programming
Introduction to PIL
Basic Network Programming
Sockets
●

●

●

●

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

●

●

●

Sockets are a basic component of network
programming.
A socket can be thought of as an information
cha...
Clients and Servers
●

●

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

●

●

A server socket object must be bound to a host
(www.usu.edu) and a port (an integer, e.g.
1234) on...
Client Sockets
●

●

●

●

A client socket object connects to a host and a
port.
The client socket object must then send a...
Transmission Control Protocol
●
●

●

●

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

●

●

UDP stands for User Datagram Protocol.
UDP is another member of the original Internet
Pr...
Minimal Server and Minimal Client
●

●

●

Write a blocking server that sets a server socket on the local
computer on port...
Module SocketServer
●

●

●

●

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

●

●

The SocketServer framework provides
request handlers.
Each time a server gets a request (a
co...
TCPServer and StreamRequestHandler
●

●

●

●

The TCPServer class allows you to create
TCP servers.
To handle requests on...
Minimal TCP Server
●

●

Write a blocking TCP server that sets a server socket on
the local computer on port 1234 and goes...
Handling Multiple Connections
●

●
●

●

There are three ways of handling multiple connections:
forking, threading, and as...
Handling Multiple Connections
●

●
●

●
●

Forking is resource-intensive, because each forked
process requires its own mem...
Handling Multiple Connections
●

●

Some operating systems (Windows) do not support
forking.
If your system supports forki...
Minimal Forking Server

minimal_forking_server.py
Minimal Threading Server

minimal_threading_server.py
URL Access
●

●

●

Python has two modules for processing URLs: urllib
and urllib2.
The urllib module is more basic and mo...
Opening Remote and Local Files
●

●

●

The urllib object has the function urlopen that allows
you to open remote and loca...
Getting HTML Source
●

●

●

The module urllib enables you to retrieve the html
source of a URL.
The retrieved html source...
Getting Ready to Take Your PIL
Installation
●

●

●

PIL is available at www.pythonware.org
There are PIL versions for various Python releases
2.7, 2.6, ...
PIL on 64bit Win
●

●

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

●

●

In order to install PIL on a Mac it needs to be built from the
source
Source is at http://www.pythonwa...
Checking PIL Availability
●

Run this program interactively in the interpreter or
or as a program:
import Image
im = Image...
Reading
Ch. 14 in Beginning Python by Hetland.
Upcoming SlideShare
Loading in …5
×

Python lecture 11

211 views

Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

Python lecture 11

  1. 1. CS 3430: Introduction to Python and Perl Lecture 11 Department of Computer Science Utah State University
  2. 2. Outline ● ● Basic Network Programming Introduction to PIL
  3. 3. Basic Network Programming
  4. 4. 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.
  5. 5. 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.
  6. 6. 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.
  7. 7. 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
  8. 8. 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.
  9. 9. 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.
  10. 10. 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.
  11. 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
  12. 12. 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.
  13. 13. 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.
  14. 14. 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.
  15. 15. 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.
  16. 16. 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.
  17. 17. 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.
  18. 18. 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.
  19. 19. Minimal Forking Server minimal_forking_server.py
  20. 20. Minimal Threading Server minimal_threading_server.py
  21. 21. 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.
  22. 22. 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.
  23. 23. 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.
  24. 24. Getting Ready to Take Your PIL
  25. 25. 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
  26. 26. 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-pilpython-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)
  27. 27. 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
  28. 28. 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
  29. 29. Reading Ch. 14 in Beginning Python by Hetland.

×