1 / 21

Introduction

Network Programming using Python Sudhir Valsange svalsan@g.clemson.edu. Introduction. •Network programming is a major use of Python •Python standard library has wide support for network

alexa
Download Presentation

Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Network Programming usingPythonSudhirValsange svalsan@g.clemson.edu

  2. Introduction •Network programming is a major use of Python •Python standard library has wide support for network protocols, data encoding/decoding, and other things you need to make it work •Writing network programs in Python tends to be substantially easier than in C/C++

  3. Prerequisites • Python Basics • Don’t need to be an expert on all of its advanced features • Some prior knowledge of networking concepts

  4. Socket • Bidirectional communication endpoint for sending and receiving data • It is defined by IP address and port number • Socket behaves much like a low level file descriptor

  5. Using Socket • To create a socket import socket s = socket(addr_family, type) • Address families socket.AF_INET Internet protocol (IPv4) socket.AF_INET6 Internet protocol (IPv6) • Socket types SOCK_STREAM Connection based stream (TCP) SOCK_DGRAM Datagrams (UDP) • Example from socket import * s = socket(AF_INET,SOCK_STREAM)

  6. TCP Client • How to make an outgoing connection • from socket import * • s = socket(AF_INET,SOCK_STREAM) • # Connect makes connection • s.connect(("www.python.org",80)) • # Send() and recv() to transmit and receive data • s .send(“Hello") • data = s.recv(10000) • # Close() shuts down the connection • s.close()

  7. TCP Server • Simple Server • from socket import * • s = socket(AF_INET,SOCK_STREAM) • s.bind(("",9000)) • s.listen(5) • while True: • c,a= s.accept() • print "Received connection from", a • c.send("Hello”) • c.close()

  8. TCP Server • Address binding • from socket import * • s = socket(AF_INET,SOCK_STREAM) • s.bind(("",9000)) • s.listen(5) • while True: • c,a= s.accept() • print "Received connection from", a • c.send("Hello”) • c.close() Binds the socket to a specific address

  9. TCP Server • Start listening for connections • from socket import * • s = socket(AF_INET,SOCK_STREAM) • s.bind(("",9000)) • s.listen(5) • while True: • c,a= s.accept() • print "Received connection from", a • c.send("Hello”) • c.close() Tells operating system to start listening for connections on the socket

  10. TCP Server • Accepting a new connection • from socket import * • s = socket(AF_INET,SOCK_STREAM) • s.bind(("",9000)) • s.listen(5) • while True: • c,a= s.accept() • print "Received connection from", a • c.send("Hello”) • c.close() Accept a new client connection

  11. TCP Server • Client socket and address • from socket import * • s = socket(AF_INET,SOCK_STREAM) • s.bind(("",9000)) • s.listen(5) • while True: • c,a= s.accept() • print "Received connection from", a • c.send("Hello”) • c.close() Accept returns a pair (client_socket,addr)

  12. TCP Server • Sending data • from socket import * • s = socket(AF_INET,SOCK_STREAM) • s.bind(("",9000)) • s.listen(5) • while True: • c,a= s.accept() • print "Received connection from", a • c.send("Hello”) • c.close() Send data to client

  13. TCP Server • Closing connection • from socket import * • s = socket(AF_INET,SOCK_STREAM) • s.bind(("",9000)) • s.listen(5) • while True: • c,a= s.accept() • print "Received connection from", a • c.send("Hello”) • c.close() Close client connection

  14. Sockets as Files • Sometimes it is easier to work with sockets as a "file" object • f = s.makefile() • This will wrap a socket with a file-like API • f.read() • f.readline() • f.write() • f.writelines() • for line in f: • ... • f.close()

  15. Sockets and Concurrency • Servers usually handle multiple clients • Each client gets its own socket on server • • To manage multiple clients, • • Server must always be ready to accept new connections • • Must allow each client to operate independently

  16. Threaded Server • • Each client is handled by a separate thread • import thread • from socket import * • defhandle_client(c,a): • ... whatever ... • c.close() • return • s = socket(AF_INET,SOCK_STREAM) • s.bind(("",9000)) • s.listen(5) • while True: • c,a= s.accept() • print ‘Connected from ’,a • thread.start_new_thread( handle_client, (c,a) )

  17. Forking Server • • Each client is handled by a subprocess • import os • from socket import * • s = socket(AF_INET,SOCK_STREAM) • s.bind(("",9000)) • s.listen(5) • while True: • c,a= s.accept() • if os.fork() == 0: • # Child process. Manage client • ... • c.close() • os._exit(0) • else: • # Parent process. Clean up and go • # back to wait for more connections • c.close()

  18. Asynchronous Server • • Server handles all clients in an event loop • import select • from socket import * • s = socket(AF_INET,SOCK_STREAM) • ... • clients = [] # List of all active client sockets • while True: • # Look for activity on any of my sockets • input,output,err= select.select(s+clients, clients, clients) • # Process all sockets with input • for i in input: • ... • # Process all sockets ready for output • for o in output: • …

  19. Utility Functions • Get the hostname of the local machine • >>> socket.gethostname() • 'foo.bar.com' • >>> • Get the IP address of a remote machine • >>> socket.gethostbyname("www.python.org") • '82.94.237.218' • >>> • Get name information on a remote IP • >>> socket.gethostbyaddr("82.94.237.218") • ('dinsdale.python.org', [ ], ['82.94.237.218']) • >>>

  20. Exercise –Building Simple Chat Server

  21. THANK YOU

More Related