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

Slides:



Advertisements
Similar presentations
Umut Girit  One of the core members of the Internet Protocol Suite, the set of network protocols used for the Internet. With UDP, computer.
Advertisements

CS3505 The Internet and Info Hiway transport layer protocols : TCP/UDP.
Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
Computer Communication Digital Communication in the Modern World Transport Layer Multiplexing, UDP
Chapter 3: Transport Layer
1 Netcomm Recitation 1: Sockets Communication Networks Recitation 1.
Socket Programming.
1 Netcomm Sockets Communication Networks Recitation 1.
Transport Layer3-1 Transport Layer Our goals: r understand principles behind transport layer services: m multiplexing/demultipl exing m reliable data transfer.
CS3771 Today: network programming with sockets  Previous class: network structures, protocols  Next: network programming Sockets (low-level API) TODAY!
Lecture 8 Chapter 3 Transport Layer
McGraw-Hill©The McGraw-Hill Companies, Inc., 2004 Application Layer PART VI.
1 School of Computing Science Simon Fraser University CMPT 771/471: Internet Architecture and Protocols Socket Programming Instructor: Dr. Mohamed Hefeeda.
1 Computer Networks Transport Layer Protocols. 2 Application-layer Protocols Application-layer protocols –one “piece” of an app –define messages exchanged.
8-1 Transport Layer Our goals: r understand principles behind transport layer services: m multiplexing/demultipl exing m reliable data transfer m flow.
Process-to-Process Delivery:
CPSC 441 TUTORIAL – JANUARY 18, 2012 TA: MARYAM ELAHI INTRODUCTION TO SOCKET PROGRAMMING WITH C.
Operating Systems Chapter 9 Distributed Communication.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
Chapter 17 Networking Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings.
Review: –What is AS? –What is the routing algorithm in BGP? –How does it work? –Where is “policy” reflected in BGP (policy based routing)? –Give examples.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
Data Communications and Computer Networks Chapter 3 CS 3830 Lecture 12 Omar Meqdadi Department of Computer Science and Software Engineering University.
Discussion 2 Sockets Programming Applets TCP UDP HTTP Delay Estimation
Application Layer 2-1 ESERCITAZIONE SOCKET PROGRAMMING.
Vassil Roussev 2 A socket is the basic remote communication abstraction provided by the OS to processes. controlled by operating system.
ICOM 6115©Manuel Rodriguez-Martinez ICOM 6115 – Computer Networks and the WWW Manuel Rodriguez-Martinez, Ph.D. Lecture 26.
2: Application Layer1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
Fall 2005 By: H. Veisi Computer networks course Olum-fonoon Babol Chapter 6 The Transport Layer.
Transport Layer1 Ram Dantu (compiled from various text books)
Lecture91 Administrative Things r Return homework # 1 r Review some problems in homework # 1 r Questions about grading? Yona r WebCT for CSE245 is working!
Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming.
Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
Chapter 2 Applications and Layered Architectures Sockets.
The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles m We will look at using sockets in C.
4061 Session 25 (4/17). Today Briefly: Select and Poll Layered Protocols and the Internets Intro to Network Programming.
Overview of TCP/IP protocols –Application layer (telnet, ssh, http, ftp, etc) The things that we use daily. –Transport layer (TCP, UDP) Allows processes.
CS 158A1 1.4 Implementing Network Software Phenomenal success of the Internet: – Computer # connected doubled every year since 1981, now approaching 200.
Transport Layer 3-1 Chapter 3 Outline r 3.1 Transport-layer services r 3.2 Multiplexing and demultiplexing r 3.3 Connectionless transport: UDP.
Chapter 3: Transport Layer Our goals: r understand principles behind transport layer services: m multiplexing/demultipl exing m reliable data transfer.
University of Calgary – CPSC 441.  A socket is an interface between the application and the network (the lower levels of the protocol stack)  The application.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
Socket Programming Introduction. Socket Definition A network socket is one endpoint in a two-way communication flow between two programs running over.
Transport Layer3-1 Chapter 3 Transport Layer Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim Kurose, Keith Ross Addison-Wesley,
Socket Programming.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
Advanced UNIX programming Fall 2002, lecture 16 Instructor: Ashok Srinivasan Acknowledgements: The syllabus and power point presentations are modified.
MULTIPLEXING/DEMULTIPLEXING, CONNECTIONLESS TRANSPORT.
LECTURE 10 Networking. NETWORKING IN PYTHON Many Python applications include networking – the ability to communicate between multiple machines. We are.
UNIX Sockets Outline UNIX sockets CS 640.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
Transport Layer3-1 Chapter 3: Transport Layer Our goals: r understand principles behind transport layer services: m multiplexing/demultipl exing m reliable.
1 Network Communications A Brief Introduction. 2 Network Communications.
Introduction 1-1 source application transport network link physical HtHt HnHn M segment HtHt datagram destination application transport network link physical.
2: Transport Layer 11 Transport Layer 1. 2: Transport Layer 12 Part 2: Transport Layer Chapter goals: r understand principles behind transport layer services:
1 K. Salah Application Layer Module K. Salah Network layer duties.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
CSEN 404 Transport Layer I Amr El Mougy Lamia Al Badrawy.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
SOCKET PROGRAMMING Presented By : Divya Sharma.
Introduction to Networks
Lecture 10 Networking.
Introduction to Networks
Transport layer API: Socket Programming
CSC Advanced Unix Programming, Fall 2015
Network Programming in Python
Network Programming in Python
Presentation transcript:

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

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 layerHDR

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

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

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

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)

Socket Programming API Application 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 with buffers, variables socket host or server process TCP with buffers, variables socket host or server Internet controlled by OS controlled by app developer

Socket: Conceptual View socket()

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 socket Dest. App socket D1 D3 D2

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! Hope you’re well

Thus, TCP needs application-level message boundary. – By carrying length in application-level header Types of Sockets (cont’d) 3Hi!16Ho…well Bytes stream

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

A Socket-eye view of the Internet Each host machine has an IP address When a packet arrives at a host cse.unr.edu ( ) cluster.cs.columbia.edu ( , , , ) newworld.cs.umass.edu ( )

Ports Port 0 Port 1 Port 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

Connectionless Services socket() bind() recvfrom() sendto() [blocked] SERVER socket() bind() recvfrom() [blocked] sendto() CLIENT

Simple Connectionless Server from socket import socket, AF_INET, SOCK_DGRAM s = socket(AF_INET, SOCK_DGRAM) s.bind((' ', 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

Simple Connectionless Client from socket import socket, AF_INET, SOCK_DGRAM s = socket(AF_INET, SOCK_DGRAM) s.bind((' ', 0)) # OS chooses port print "using", s.getsocketname() server = (' ', 11111) s.sendto("MixedCaseString", server) data, addr = s.recvfrom(1024) print "received", data, "from", addr s.close()

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 connect ion – step 4: data transfer Passive Participant l-socka-sock-1a-sock-2 Active 1 socket Active 2 socket

TCP state diagram

Connection-Oriented Services socket() bind() listen() accept() read() write() [blocked] socket() connect() write() read() [blocked] ServerClient When interaction is over, server loops to accept a new connection

Connection-Oriented Server from socket import \ socket, AF_INET, SOCK_STREAM s = socket(AF_INET, SOCK_STREAM) s.bind((' ', 9999)) s.listen(5) # max queued connections while 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

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()

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 Big-Endian machine Little-Endian machine WRONG!!!

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

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

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 …

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

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)

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

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