Introduction to Python Network Programmingclnv.s3.amazonaws.com/2017/eur/pdf/DEVNET-1040.pdf ·...
Transcript of Introduction to Python Network Programmingclnv.s3.amazonaws.com/2017/eur/pdf/DEVNET-1040.pdf ·...
Introduction to Python Network Programming for Network Architects and Engineers
Vince Kelly TSA
Session ID: DEVNET-1040
• Python Basics
• The Python Socket Module
• Security
• ‘Higher Layer’ Protocols & APIs: XMLRPC
• Higher Layer’ Protocols: REST & HTTP
• Conclusion
Agenda
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 4DEVNET-1040
Python Basics
• Working with the Command Line
• Python Basics: Working With files, Directories and Strings
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 5DEVNET-1040
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
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 6DEVNET-1040
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
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 7DEVNET-1040
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
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 8DEVNET-1040
‘Higher Layer’ Protocols & APIs
• Building A Spreadsheet Primer
• XMLRPC
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 9DEVNET-1040
‘Higher Layer’ Protocols: REST & HTTP
• REST and HTTP
• TCP Send a REST Request to Google
• ‘Screen Scraping’ using Python URLLIB
• APIC REST Interface
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
WHAT’S THE POINT of ALL THIS??
“Good Programmers write code, GREAT Programmers steal code”DEVNET-1040 10
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1040 11
Follow Along:
followCmd_line.py Parsing command line
followFiles.py Working with files, directories and strings
followFile_Info.py Retrieves file size, creation, modified and accessed on a file
followFile_Encrypt.py Encodes content and writes to disk
followFile_Decrypt.py Decodes file and prints to screen
followPing.py Detect what OS we are on, Make system call to execute ping (windows)
Python Basics
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
Start python
Interpreter(python.exe)
Find & execute
this program (Program_xyz.py)
argv = [ ‘Program_xyz.py’, ‘www.cisco.com’, ‘192.168.10.1’ ]
Save whatever comes
after the program
name
as command line
argumentS(www.cisco.com,
192.168.10.1)
print argv[0]
‘Program_xyz.py’
print argv[1]
‘www.cisco.com’
print argv[2]
‘192.168.10.1’
Python Basics: Working with the Command Line
DEVNET-1040 12
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1040 13
- 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
followCmd_Line.py
Python Basics: Working with the Command Line
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1040 14
Python Basics: Working With files, Directories and Strings
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1040 15
- 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
followFiles.py
Python Basics: Working With files, Directories and Strings
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1040 16
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1040 17
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
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1040 18
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
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1040 19
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
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1040 20
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
Network Communications
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
Operating System
App
Pytho
n
Enviro
nment
Web
Browse
r
Operating System
Data
Warehou
se
Pytho
n
Enviro
nment
Web
Service
s
SERVER
Operating System
App
Pytho
n
Enviro
nment
Web
Browse
r
CLIENT
CLIENT
Communications
Stack(s)
DEVNET-1040 22
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
IP
TCP UDP
XMLRPC
FTP
20,21
SNMP
161
Device Drivers
Pyth
on
So
cket
Lib
rary
“Higher Layer” Libraries
HTTP, etc.
NIC
B
U
STx
Buffers
Rx
Buffers
Framer/
Protocol
decode
Telnet
23
SMTP
25
DNS
53
TC
P/I
P P
RO
TO
CO
L S
TA
CK
LAYER 1
ACI APICFirefox
Browser
Web
80
Sockets: What Can We Get Just From the Python Standard Library Alone?
DEVNET-1040 23
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1040 24
Follow Along:
followSocket_Examples.py Displays local socket information
followSock_Scan1.py Scans remote services
followDisplay_SocketProtocols.py Displays basic IPv4 and IPv6 stack information
followUDP_Client.py Basic UDP Client
followUDP_Server.py Basic UDP Server
followUDP_ClientEncoded.py Receives an encoded ROT-13 message and decodes it
followUDP_CountDownServer.py Broadcasts an encoded message
Python Socket Library
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1040 25
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',
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
IP
TCP UDP
XMLRPC
FTP
20,21
SNMP
161
Device Drivers
Pyth
on
So
cket
Lib
rary
“Higher Layer” Libraries
HTTP, etc.
NIC
B
U
STx
Buffers
Rx
Buffers
Framer/
Protocol
decode
Telnet
23
SMTP
25
DNS
53
TC
P/I
P P
RO
TO
CO
L S
TA
CK
LAYER 1
ACI APICFirefox
Browser
Web
80
Sockets: Let’s Start with UDP
DEVNET-1040 26
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
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.
27DEVNET-1040
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1040 28
socket.getaddrinfo("192.168.252.253", 80, 0, 0, socket.SOL_TCP)
[ (2, 0, 6, '', ('192.168.252.253', 80)) ]
Family Protocol
Type
socket.getaddrinfo('2a04:4e42:5::223',80, 0, 0, socket.SOL_TCP)
[(23, 0, 6, '', ('2a04:4e42:5::223', 80, 0, 0))]
IPv6
TCP
Socket
Type
Canonical
Name
Socket Name: 2 Tuple
Socket Name: 6 Tuple
ScopeFlow LabelCanonical
Name
1 STREAM,
2 DATAGRAM,
3 RAW,
4 RDM,
5 SEQPACKET
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1040 29
But Before We Jump In, a B R I E F Primer on OOP versus POP…….
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
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?
30DEVNET-1040
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 31DEVNET-1040
import socket
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Socket Objects
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
Python socket module’s METHOD
Retrieve (and use) the Python socket module
What do we mean by a ‘socket object’?
New socket object
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 32DEVNET-1040
Sockets
‘c’
Socket objectSocket 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
client
import socket
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c.connect((‘www.cisco.com’,80))
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 36DEVNET-1040
Sockets
server
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverAddr = S.gethostname()
s.bind((serverAddr,12345))
s.listen(5)
NIC
10.255.88.76
while 1:
client, cleintAddr = s.accept()
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
Hello Server!!! TCP | IP
37DEVNET-1040
Sockets
server
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverAddr = ‘10.255.88.76’
s.bind((serverAddr,12345))
s.listen(5)
NIC
10.255.88.76
‘c’
Socket object
client
import socket
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
NIC
192.168.19.5
‘client’
Socket object
while 1:
client, clientAddr = s.accept()c.send(‘Hello Server!!!’)
client 192.168.13.5
print “I just got a connection request from: “, clientAddr
client.close()
c.shutdown(socket.SHUT_RDWR))
c.connect((‘10.255.88.76’,80)
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
‘client’
Socket object3
‘client’
Socket object2
Hello Server!!! TCP | IP
38DEVNET-1040
Sockets
server
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverAddr = s.gethostname()
s.bind((serverAddr,12345))
s.listen(5)
NIC
10.255.88.76
‘c’
Socket object
client
import socket
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c.connect((‘www.cisco.com’,80))
NIC
192.168.19.5
‘client’
Socket object
while 1:
client, clientAddr = s.accept()c.send(‘Hello Server!!!’)
client 192.168.13.5
client2 10.23.11.254
17.255.10.1client3
print “I just got a connection request from: “, clientAddr
client.close()
c.shutdown(socket.SHUT_RDWR)
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1040 39
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'
Sockets
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-1040 40
El-cheapo_UDP_client.py:
import socket,time
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # spin up socket object
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) # 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
Sockets
Thank You
Q & A
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
Complete Your Online Session Evaluation
Don’t forget: Cisco Live sessions will be available for viewing on-demand after the event at CiscoLive.com/Online
• 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
43DEVNET-1040
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
Continue Your Education
• Demos in the Cisco campus
• Walk-in Self-Paced Labs
• Lunch & Learn
• Meet the Engineer 1:1 meetings
• Related sessions
44DEVNET-1040
Thank You