Introduction to Python Network Programming for Network Architects and Engineers Vince Kelly TSA Session ID: DEVNET-1040
Agenda Python Basics The Python Socket Module Security Higher Layer Protocols & APIs: XMLRPC Higher Layer Protocols: REST & HTTP Conclusion
Python Basics Working with the Command Line Python Basics: Working With files, Directories and Strings DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 4
Sockets: What Can We Get Just From the Python Standard Library Alone? Python Socket Library Starting Out With UDP What Are Sockets? B R I E F Primer on OOP versus POP The Big Five Questions to Answer Socket Objects Socket Names DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 5
Sockets: Moving on To TCP Parsing Directories and Files with OS.Walk Simple Message Passing Using TCP Simple File Transfer Using TCP Simple File Transfer With Directory Search Using TCP DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 6
SECURITY One-way Hash Creation Display All Available Hash Algorithms Generate a One-way Hash for a File Verifying a File Has Not Been Tampered With Step 1: Client Side, Server Side Verifying a File Has Not Been Tampered With Step 2 Client Side, Server Side Establish Client/Server Connection Over SSL Client Side, Server Side DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 7
Higher Layer Protocols & APIs Building A Spreadsheet Primer XMLRPC DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 8
Higher Layer Protocols: REST & HTTP REST and HTTP TCP Send a REST Request to Google Screen Scraping using Python URLLIB APIC REST Interface DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 9
WHAT S THE POINT of ALL THIS?? Good Programmers write code, GREAT Programmers steal code DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 10
Python Basics Follow Along: followcmd_line.py followfiles.py followfile_info.py followfile_encrypt.py followfile_decrypt.py followping.py Parsing command line Working with files, directories and strings Retrieves file size, creation, modified and accessed on a file Encodes content and writes to disk Decodes file and prints to screen Detect what OS we are on, Make system call to execute ping (windows) DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 11
Python Basics: Working with the Command Line Start python Interpreter (python.exe) Find & execute this program (Program_xyz.py) Save whatever comes after the program name as command line arguments (www.cisco.com, 192.168.10.1) argv = [ Program_xyz.py, www.cisco.com, 192.168.10.1 ] print argv[0] print argv[1] print argv[2] Program_xyz.py www.cisco.com 192.168.10.1 DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 12
Python Basics: Working with the Command Line followcmd_line.py - Uses built-in SYS module: sys.argv() and len() built-in methods - Displays: - The programs (empty) command line, number of arguments, etc - Appends Hello World to the command line - The programs (non-empty command line, number of arguments, etc - Removes the last entry from the cmd line - Example of Type Casting a string value entered by user into an integer DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 13
Python Basics: Working With files, Directories and Strings DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 14
Python Basics: Working With files, Directories and Strings followfiles.py - Uses built-in os module: os.getcwd() built-in method and.lower() string method - Concatenates IP address, User Name and Password entered by user - Calls a function with the concatenated message as a parameter - Takes directory and file name from user (or defaults to CWD) - Opens file in append mode - Loops waiting forever on more input from user DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 15
DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 16
Python Basics: Working With files, Directories and Strings followfile_info.py - Uses built-in getsize(),.stat() built-in methods - Uses time.ctime() to return file creation, modification and last access DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 17
Python Basics: Working With files, Directories and Strings followencrypt.py - Uses built-in codecs and os modules: - Encodes user input with ROT-13 - Loops forever on user input DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 18
Python Basics: Working With files, Directories and Strings followdecrypt.py - Uses built-in codecs and os modules: - Decodes user input using ROT-13 - Loops forever on user input DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 19
Python Basics: Working With files, Directories and Strings followping.py - Uses built-in os module: os.name() returns the operating system ('posix', 'nt', 'mac', 'os2', 'ce', 'java', 'riscos ) - Makes system call to execute ping and display the results - Displays warning if host doesn t respond after timeout - Loops forever on user input DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 20
Network Communications
SERVER CLIENT Data Warehou se Pytho n Enviro nment Web Service s App Pytho n Enviro nment Operating System Web Browse r Communications Stack(s) Operating System CLIENT App Pytho n Enviro nment Web Browse r Operating System DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 22
Python Socket Library TCP/IP PROTOCOL STACK Sockets: What Can We Get Just From the Python Standard Library Alone? ACI APIC XMLRPC Firefox Browser Higher Layer Libraries HTTP, etc. Web 80 FTP 20,21 Telnet 23 SMTP 25 SNMP 161 DNS 53 TCP UDP IP Device Drivers Tx Buffers B U S Rx Buffers Framer/ Protocol decode LAYER 1 NIC DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 23
Python Socket Library Follow Along: followsocket_examples.py followsock_scan1.py followdisplay_socketprotocols.py followudp_client.py followudp_server.py followudp_clientencoded.py followudp_countdownserver.py Displays local socket information Scans remote services Displays basic IPv4 and IPv6 stack information Basic UDP Client Basic UDP Server Receives an encoded ROT-13 message and decodes it Broadcasts an encoded message DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 24
gethostname() return the current hostname gethostbyname() map a hostname to its IP number 'gethostbyname_ex() IPv4 Only gethostbyaddr() -- map an IP number or hostname to DNS info getservbyname() map a service name and a protocol name to a port number getprotobyname() map a protocol name (e.g. 'tcp') to a number getaddrinfo() getfqdn() getnameinfo() getservbyport() socket.setdefaulttimeout() set the default timeout value socket.getdefaulttimeout() get the default timeout value 'has_ipv6', DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 25
Sockets: Let s Start with UDP Python Socket Library TCP/IP PROTOCOL STACK ACI APIC XMLRPC Firefox Browser Higher Layer Libraries HTTP, etc. Web 80 FTP 20,21 Telnet 23 SMTP 25 SNMP 161 DNS 53 TCP UDP IP Device Drivers Tx Buffers B U S Rx Buffers Framer/ Protocol decode LAYER 1 NIC DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 26
What Are Sockets? Is an abstract concept that represents an end point. Programs use sockets to communicate with other programs which may or may not be on the same computer. A socket is defined by IP address, the port it listens on and the protocol used Client/Server sockets: represent endpoints of a conversation. Server sockets just produce more client sockets e.g., Web Browser uses a client socket, Web server uses a server socket to listen on.. To create a client/server connection you: Create/ spin up a socket object. This contains all the methods needed to communicate Bind to the port you want. If successful, the socket exists for the length of the session.. Client sockets are normally only used for one exchange (or a small set of sequential exchanges). They are created and torn down frequently. DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 27
socket.getaddrinfo("192.168.252.253", 80, 0, 0, socket.sol_tcp) Socket Name: 2 Tuple [ (2, 0, 6, '', ('192.168.252.253', 80)) ] Family Socket Type Protocol Type Canonical Name socket.getaddrinfo('2a04:4e42:5::223',80, 0, 0, socket.sol_tcp) Socket Name: 6 Tuple [(23, 0, 6, '', ('2a04:4e42:5::223', 80, 0, 0))] IPv6 1 STREAM, 2 DATAGRAM, 3 RAW, TCP Canonical Name Flow Label Scope 4 RDM, 5 SEQPACKET DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 28
But Before We Jump In, a B R I E F Primer on OOP versus POP. DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 29
The Big Five Questions to Answer: 1) What kind of network do we want to talk to? What address family? IPv4 (AF_INET) or IPv6 (AF_INET6), etc. 2) What type of connection do we want? Do we want a datagram service (UDP) or a connection oriented stream service (TCP)? 3) What kind of protocol do we need? Answers 1 and 2 already narrowed this down 4) What IP address to use? 5) What UDP or TCP port number should we use? DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 30
Socket Objects Retrieve (and use) the Python socket module import socket Python socket module s METHOD c = socket.socket(socket.af_inet, socket.sock_stream) New socket object What Protocol Stack Version? INET: use IPv4 INET6: use IPv6 What Part of the Protocol Stack Do You Want? SOCK_STREAM: use TCP SOCK_DGRAM: use UDP What do we mean by a socket object? DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 31
Sockets client import socket c = socket.socket(socket.af_inet, socket.sock_stream) c.connect(( www.cisco.com,80)) Socket object c Socket object - Gets instantiated/ spun up (on each side) through the python socket module - Handles setting up the session - Handles sending & receiving data - Handles Error recovery - Handles ending/tearing down the session DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 32
Sockets server import socket s = socket.socket(socket.af_inet, socket.sock_stream) serveraddr = S.gethostname() s.bind((serveraddr,12345)) s.listen(5) while 1: client, cleintaddr = s.accept() 10.255.88.76 NIC DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 36
Sockets server import socket s = socket.socket(socket.af_inet, socket.sock_stream) import socket client c = socket.socket(socket.af_inet, socket.sock_stream) serveraddr = 10.255.88.76 s.bind((serveraddr,12345)) s.listen(5) client 192.168.13.5 c.connect(( 10.255.88.76,80) c.send( Hello Server!!! ) c.shutdown(socket.shut_rdwr)) Socket object Socket object while 1: client, clientaddr = s.accept() c client print I just got a connection request from:, clientaddr client.close() Hello Server!!! TCP IP 192.168.19.5 NIC 10.255.88.76 NIC DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 37
Sockets server import socket s = socket.socket(socket.af_inet, socket.sock_stream) import socket client c = socket.socket(socket.af_inet, socket.sock_stream) serveraddr = s.gethostname() s.bind((serveraddr,12345)) s.listen(5) client 192.168.13.5 c.connect(( www.cisco.com,80)) c.send( Hello Server!!! ) c.shutdown(socket.shut_rdwr) Socket object c Socket object3 Socket object2 Socket object client client client client2 10.23.11.254 client3 17.255.10.1 while 1: client, clientaddr = s.accept() print I just got a connection request from:, clientaddr client.close() Hello Server!!! TCP IP 192.168.19.5 NIC 10.255.88.76 NIC DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 38
Sockets El-cheapo_UDP_server.py: import socket,sys sp = socket.socket(socket.af_inet, socket.sock_dgram) sp.setsockopt(socket.sol_socket, socket.so_broadcast, 1) count = 0 port = 5678 dest = ('<broadcast>', port) print 'Sending message...' while True: msg = 'Hello World' sp.sendto(msg, dest) # send message out on port 5678 to address 'ffffff' DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 39
Sockets El-cheapo_UDP_client.py: import socket,time s=socket.socket(socket.af_inet,socket.sock_dgram) s.setsockopt(socket.sol_socket, socket.so_broadcast, 1) # spin up socket object # tell it to use broadcasts s.bind(('',5678)) # 'glue' this program onto any address using port 5678 print "waiting..." data,addr = s.recvfrom(1024) # get a message up to to 1024 bytes - and where it came from print '\r', data, addr # display the message DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 40
Thank You
Q & A
Complete Your Online Session Evaluation Please complete your Online Session Evaluations after each session Complete 4 Session Evaluations & the Overall Conference Evaluation (available from Thursday) to receive your Cisco Live T-shirt All surveys can be completed via the Cisco Live Mobile App or the Communication Stations Don t forget: Cisco Live sessions will be available for viewing on-demand after the event at CiscoLive.com/Online DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 43
Continue Your Education Demos in the Cisco campus Walk-in Self-Paced Labs Lunch & Learn Meet the Engineer 1:1 meetings Related sessions DEVNET-1040 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 44
Thank You