Introduction to Python Network Programming for Network Architects and Engineers

Similar documents
Networking. Chapter How to identify machines on internet Ports

Network Programming in Python. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

NXOS in the Real World Using NX-API REST

4.5 Accessing Networks

SOCKET. Valerio Di Valerio

LECTURE 10. Networking

The Application Layer: Sockets Wrap-Up

Network Programming in Python. based on Chun, chapter 2; plus material on classes

ECE 435 Network Engineering Lecture 2

A Client-Server Exchange

IEMS 5780 / IERG 4080 Building and Deploying Scalable Machine Learning Services

UNIX Sockets. Developed for the Azera Group By: Joseph D. Fournier B.Sc.E.E., M.Sc.E.E.

A set of processes distributed over a network that communicate via messages. Processes communicate via services offered by the operating system

TRex Realistic Traffic Generator

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

CptS 360 (System Programming) Unit 17: Network IPC (Sockets)

CS321: Computer Networks Introduction to Application Layer

Lecture 2. Outline. Layering and Protocols. Network Architecture. Layering and Protocols. Layering and Protocols. Chapter 1 - Foundation

Distributed Systems. 02. Networking. Paul Krzyzanowski. Rutgers University. Fall 2017

Interconnecting Networks with TCP/IP. 2000, Cisco Systems, Inc. 8-1

ECE 435 Network Engineering Lecture 2

LESSON PLAN. Sub. Code & Name : IT2351 & Network Programming and Management Unit : I Branch: IT Year : III Semester: VI.

Context. Distributed Systems: Sockets Programming. Alberto Bosio, Associate Professor UM Microelectronic Departement

Coding Getting Started with Python

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

Foundations of Python

Application Programming Interfaces

Different Layers Lecture 21

Pemrograman Jaringan Network Client

Introduction to Computer Systems. Networks 2. c Theodore Norvell. The Sockets API

NETWORK PROGRAMMING AND MANAGEMENT 1 KINGS DEPARTMENT OF INFORMATION TECHNOLOGY QUESTION BANK

Contiv installation and integration with ACI

UNIX Network Programming. Overview of Socket API Network Programming Basics

Insights into your WLC with Wireless Streaming Telemetry

Your API Toolbelt Tools and techniques for testing, monitoring, and troubleshooting REST API requests

CSE 333 SECTION 8. Sockets, Network Programming

Hands-On with IoT Standards & Protocols

Get Hands On With DNA Center APIs for Managing Intent

We will cover in this order: 2.1, 2.7, 2.5, 2.4, 2.2

Concept Questions Demonstrate your knowledge of these concepts by answering the following questions in the space provided.

Automation with Meraki Provisioning API

CSE 333 SECTION 7. C++ Virtual Functions and Client-Side Network Programming

Interconnecting Networks with TCP/IP

Sockets 15H2. Inshik Song

Network Security. Introduction to networks. Radboud University, The Netherlands. Autumn 2015

CSMC 412. Computer Networks Prof. Ashok K Agrawala Ashok Agrawala Set 2. September 15 CMSC417 Set 2 1

Chapter 7. Local Area Network Communications Protocols

CS321: Computer Networks Socket Programming

CS 351 Design of Large Programs Java and Socket Communication

CSE 333 SECTION 7. Client-Side Network Programming

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

The Application Layer: Sockets Wrap-Up

UNIT IV- SOCKETS Part A

Overview. Exercise 0: Implementing a Client. Setup and Preparation

NT1210 Introduction to Networking. Unit 10

Client/Server Computing & Socket Programming

Tutorial on Socket Programming

Computer Communication Networks Socket Programming

CS321: Computer Networks FTP, TELNET, SSH

Interprocess Communication

CloudCenter for Developers

CHETTINAD COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF MCA QUESTION BANK UNIT 1

Ports under 1024 are often considered special, and usually require special OS privileges to use.

Kuber-what?! Learn about Kubernetes

Set of IP routers. Set of IP routers. Set of IP routers. Set of IP routers

Basics. Once socket is configured, applica6ons. Socket is an interface between applica6on and network

Computer Systems and Networks

CSE 333 SECTION 6. Networking and sockets

Hybrid of client-server and P2P. Pure P2P Architecture. App-layer Protocols. Communicating Processes. Transport Service Requirements

OBJECT ORIENTED PROGRAMMING

Networked Applications: Sockets. End System: Computer on the Net

CSE 333 Section 8 - Client-Side Networking

Networked Applications: Sockets. Goals of Todayʼs Lecture. End System: Computer on the ʻNet. Client-server paradigm End systems Clients and servers

Defining Networks with the OSI Model. Module 2

Computer Networks Prof. Ashok K. Agrawala

Finesse APIs: Getting started with the REST APIs and XMPP events

DevNet Workshop-Learning Cisco platform Exchange Grid (pxgrid) Dynamic Topics

Socket Programming for TCP and UDP

Magical Chatbots with Cisco Spark and IBM Watson

CSE 333 Lecture 16 - network programming intro

IPv6 Porting Applications

The Application Layer: Sockets Wrap-Up

Chapter 2 - Part 1. The TCP/IP Protocol: The Language of the Internet

Hybrid Cloud Automation using Cisco CloudCenter API

Cisco UCS Director and ACI Advanced Deployment Lab

CS UDP: User Datagram Protocol, Other Transports, Sockets. congestion worse);

Chapter 2: outline. 2.1 principles of network applications. 2.6 P2P applications 2.7 socket programming with UDP and TCP

Real time Location Services Overview and Use cases

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Configure Transmission Control Protocol (TCP) and User Datagram Protocol (UDP) Service Settings on a Switch

Contiv installation and integration with ACI. LTRCLD-2003

JAVA Network API. 2 - Connection-Oriented vs. Connectionless Communication

OSI Transport Layer. Network Fundamentals Chapter 4. Version Cisco Systems, Inc. All rights reserved. Cisco Public 1

CSE 124 Discussion Section Sockets Programming 10/10/17

Socket Programming. Omer Ozarslan

Lab Assignment 3 for ECE374

ECE4110 Internetwork Programming. Introduction and Overview

Outline. Option Types. Socket Options SWE 545. Socket Options. Out-of-Band Data. Advanced Socket. Many socket options are Boolean flags

Types (Protocols) Associated functions Styles We will look at using sockets in C Java sockets are conceptually quite similar

L1/L2 NETWORK PROTOCOL TESTING

Transcription:

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