Top Banner
Network Programming in Python Modified from •Steve Holden •Dan Rubenstein •DK Moon Mehmet Hadi Gunes
31

Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Dec 19, 2015

Download

Documents

Adrian Robbins
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Network Programmingin Python

Modified from•Steve Holden•Dan Rubenstein•DK Moon

Mehmet Hadi Gunes

Page 2: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Objectives

• Review principles of networking• Contrast TCP and UDP features• Show how Python programs access

networking functionality• Give examples of client and server program

structures• Demonstrate some Python network libraries• Give pointers to other network functionality

Page 3: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Inter-Layer Relationships

• Each layer uses the layer below– The lower layer adds headers to the data from the

upper layer– The data from the upper layer can also be a

header on data from the layer above …

PROTOCOL DATA

DATA

Upper layer

Lower layer HDR

Page 4: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

IP Characteristics

• Datagram-based– Connectionless

• Unreliable– Best efforts delivery– No delivery guarantees

• Logical (32-bit) addresses– Unrelated to physical addressing– Leading bits determine network membership

Page 5: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

UDP Characteristics

• Also datagram-based– Connectionless, unreliable, can broadcast

• Applications usually message-based– No transport-layer retries– Applications handle (or ignore) errors

• Processes identified by port number• Services live at specific ports

– Usually below 1024, requiring privilege

Page 6: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

TCP Characteristics

• Connection-oriented– Two endpoints of a virtual circuit

• Reliable– Application needs no error checking

• Stream-based– No predefined blocksize

• Processes identified by port numbers• Services live at specific ports

Page 7: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Sockets• Various sockets… Any similarity?

• Endpoint of a connection– Identified by IP address and Port number

• Primitive to implement high-level networking interfaces– e.g., Remote procedure call (RPC)

Page 8: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Socket Programming APIApplication Programming Interface

•Socket analogous to door – sending process shoves message out

door– sending process assumes transport

infrastructure on other side of door which brings message to socket at receiving process

– host-local, application created/owned, OS-controlled

– connection between sockets set-up/managed by OS

process

TCP withbuffers,variables

socket

host orserver

process

TCP withbuffers,variables

socket

host orserver

Internet

controlledby OS

controlled byapp developer

Page 9: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Socket: Conceptual Viewsocket()

Page 10: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Two essential types of sockets• SOCK_STREAM

– a.k.a. TCP– reliable delivery– in-order guaranteed– connection-oriented– bidirectional

• SOCK_DGRAM– a.k.a. UDP– unreliable delivery– no order guarantees– no notion of “connection” – app

indicates dest. for each packet– can send or receive

App

socket3 2 1

Dest.App

socket3 2 1

D1

D3

D2

Page 11: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Types of Sockets

• When sending “Hi!” and “Hope you’re well”• TCP treats them as a single bytes stream

• UDP treats them as separate messages

Hi!Hope…well

Bytes stream

Hi!Hi!Hope you’re well

Hope you’re well

Page 12: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

• Thus, TCP needs application-level message boundary.– By carrying length in application-level header

Types of Sockets (cont’d)

3Hi!16Ho…well

Bytes stream

Page 13: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Client/Server Concepts

• Server opens a specific port– The one associated with its service– Then just waits for requests– Server is the passive opener

• Clients get ephemeral ports– Guaranteed unique, 1024 or greater– Uses them to communicate with server– Client is the active opener

Page 14: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

A Socket-eye view of the Internet

• Each host machine has an IP address• When a packet arrives at a host

cse.unr.edu

(134.197.20.22)

cluster.cs.columbia.edu

(128.59.21.14, 128.59.16.7, 128.59.16.5, 128.59.16.4)

newworld.cs.umass.edu

(128.119.245.93)

Page 15: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Ports

Port 0

Port 1

Port 65535

• Each host has 65,536 ports

• Some ports are reserved for specific apps– 20,21: FTP– 23: Telnet– 80: HTTP– see RFC 1700

• about 2000 ports are reserved

A socket provides an interface to send data to/from the network through a port

Page 16: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Connectionless Services

socket()

bind()

recvfrom()

sendto()

[blocked]

SERVER

socket()

bind()

recvfrom()

[blocked]

sendto()

CLIENT

Page 17: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Simple Connectionless Server

from socket import socket, AF_INET, SOCK_DGRAM

s = socket(AF_INET, SOCK_DGRAM)

s.bind(('127.0.0.1', 11111))

while True:

data, addr = s.recvfrom(1024)

print "Connection from", addr

s.sendto(data.upper(), addr)

Note that the bind() argument is a two-element tuple of address and port number

Empty -> all

Page 18: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Simple Connectionless Client

from socket import socket, AF_INET, SOCK_DGRAM

s = socket(AF_INET, SOCK_DGRAM)

s.bind(('127.0.0.1', 0)) # OS chooses port

print "using", s.getsocketname()

server = ('127.0.0.1', 11111)

s.sendto("MixedCaseString", server)

data, addr = s.recvfrom(1024)

print "received", data, "from", addr

s.close()

Page 19: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Connection setup cont’d• Passive participant

– step 1: listen (for incoming requests)

– step 3: accept (a request)– step 4: data transfer

• The accepted connection is on a new socket

• The old socket continues to listen for other active participants

19

• Active participant

– step 2: request & establish connection

– step 4: data transfer

Passive Participant

l-socka-sock-1 a-sock-2

Active 1

socket

Active 2

socket

Page 20: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

TCP state diagram

Page 21: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Connection-Oriented Servicessocket()

bind()

listen()

accept()

read()

write()

[blocked]

socket()

connect()

write()

read()

[blocked]

[blocked]

Server Client

When interaction is over, server loops to accept a new connection

Page 22: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Connection-Oriented Server

from socket import \socket, AF_INET, SOCK_STREAM

s = socket(AF_INET, SOCK_STREAM)s.bind(('127.0.0.1', 9999))s.listen(5) # max queued connectionswhile True: sock, addr = s.accept() # use socket sock to communicate # with client process

• Client connection creates new socket

– Returned with address by accept()• Server handles one client at a time

Page 23: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Connection-Oriented Client

s = socket(AF_INET, SOCK_STREAM)s.connect((HOST, PORT))s.send('Hello, world')data = s.recv(1024)s.close()print 'Received', data

• This is a simple example– Sends message, receives response– Server receives 0 bytes after close()

Page 24: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Some socket Utility Functions

• htonl(i), htons(i)

– 32-bit or 16-bit integer to network format• ntohl(i), ntohs(i)

– 32-bit or 16-bit integer to host format• inet_aton(ipstr), inet_ntoa(packed)

– Convert addresses between regular strings and 4-byte packed strings

128.119.40.12

128

119

40 12

12.40.119.128

128

119

40 12

Big-Endianmachine

Little-Endianmachine

WRONG!!!

Page 25: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Handling Names & Addresses

• getfqdn(host='')

– Get canonical host name for host

• gethostbyaddr(ipaddr)• gethostbyname_ex(hostname)

– Returns (hostname, aliases, addresses)• Hostname is canonical name• Aliases is a list of other names• Addresses is a list of IP address strings

Page 26: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Treating Sockets as Files

• makefile([mode[, bufsize]]) – Creates a file object that references the socket– Makes it easier to program to handle data streams

• No need to assemble stream from buffers

Page 27: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Summary of Address Families

• socket.AF_UNIX– Unix named pipe (NOT Windows…)

• socket.AF_INET– Internet – IP version 4– The basis of this class

• socket.AF_INET6– Internet – IP version 6– Rather more complicated …

Page 28: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Summary of Socket Types

• socket.SOCK_STREAM– TCP, connection-oriented

• socket.SOCK_DGRAM– UDP, connectionless

• socket.SOCK_RAW – Gives access to subnetwork layer

• SOCK_RDM, SOCK_SEQPACKET– Very rarely used

Page 29: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Timeout Capabilities

• Can set a default for all sockets– socket.setdefaulttimeout(seconds)

– Argument is float # of seconds– Or None (indicates no timeout)

• Can set a timeout on an existing socket s– s.settimeout(seconds)

Page 30: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Server Libraries

• SocketServer module provides basic server features

• Subclass the TCPServer and UDPServer classes to serve specific protocols

• Subclass BaseRequestHandler, overriding its handle() method, to handle requests

• Mix-in classes allow asynchronous handling via ThreadingMixIn

Page 31: Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.

Using SocketServer Module

• Server instance created with address and handler-class as arguments:SocketServer.UDPServer(myaddr, MyHandler)

• Each connection/transmission creates a request handler instance by calling the handler-class*

• Created handler instance handles a message (UDP) or a complete client session (TCP)

* In Python you instantiate a class by calling it like a function