Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Quick Guide to
Python Socket Programming
吳俊興
國立高雄大學 資訊工程學系
Python Overview
• Philosophy - code readability
– Uses whitespace indentation
• Rather than curly braces or keywords, to delimit blocks
– Can express concepts in fewer lines of code
• Built-in support for arbitrary precision arithmetic (long, Decimal, Fractional, …)
• General-purpose, high-level programming language
– Interpreters
• Time-critical functions to extension modules written in languages such as C
– Pyinstaller to package into stand-alone executable programs
– Cython to translate a Python script into C
– APIs available in C/C++ to extend and embed the Python Interpreter
• Multiple programming paradigms
– Object-oriented, imperative (statements to change program's states) and
functional programming or procedural styles
• Features
– A dynamic type system
– An automatic memory management
– A large and comprehensive standard library
https://en.wikipedia.org/wiki/Python_(programming_language)
https://www.python.org/
2
Download
• Python Releases for Windows
https://www.python.org/downloads/windows/
• Python 3.5.0 (2015-09-13)
– Compatible to Python 2.6 and 2.7 (not to 2.0)
• print("Hello, world!")
# for 2.6 or later
print "Hello, world!“
# for versions earlier than 2.6
• raw_input() changed to input()
• Unicode text (str) v.s. data (bytes, b"...") - must always explicitly convert
• str.encode() to go from str to bytes, and bytes.decode() to go from bytes to str
• Supports Unicode characters in identifiers
– Windows x86-64 executable installer
https://www.python.org/ftp/python/3.5.0/python-3.5.0-amd64.exe
– Windows x86 executable installer
https://www.python.org/ftp/python/3.5.0/python-3.5.0.exe
• Python 2.7.10 (2015-05-23)
– Windows x86 MSI installer
https://www.python.org/ftp/python/2.7.10/python-2.7.10.msi
– Windows x86-64 MSI installer
https://www.python.org/ftp/python/2.7.10/python-2.7.10.amd64.msi
3
Install
http://docs.python.org/3.5/tutorial/
4
Python IDLE
• Integrated Development and Learning Environment
https://docs.python.org/3/library/idle.html
– coded in 100% pure Python, using the tkinter GUI toolkit
– cross-platform: works mostly the same on Windows, Unix, and Mac OS X
– Python shell window (interactive interpreter) with colorizing of code input,
output, and error messages
– multi-window text editor with multiple undo, Python colorizing, smart
indent, call tips, auto completion, and other features
– search within any window, replace within editor windows, and search
through multiple files (grep)
– debugger with persistent breakpoints, stepping, and viewing of global and
local namespaces
– configuration, browsers, and other dialogs
Ctrl-D or exit() to quit
5
Your 1st Python under IDLE
• Write simple code under Python shell
>>> print("Hello, I'm A1025501 from NUK CSIE.")
• Invoke Editor Window to develop codes and Run
File → New File
Run → Run Module (F5)
6
Quick Python Script Explanation for Programmers
http://coffeeghost.net/pybat/python_cheatsheet.png
7
Socket Programming
Goal: learn how to build client/server applications that
communicate using sockets
Socket: door between application process and endend-transport protocol
application
process
socket
application
process
transport
transport
network
network
link
physical
Internet
link
controlled by
app developer
controlled
by OS
physical
8
Socket Types
Two socket types for two transport services:
– UDP: unreliable datagram
– TCP: reliable, byte stream-oriented
Application Example:
1.
Client reads a line of characters (data) from its
keyboard and sends the data to the server
2.
The server receives the data and converts
characters to uppercase
3.
The server sends the modified data to the client
4.
The client receives the modified data and displays
the line on its screen
9
Socket Programming with UDP
UDP: no “connection" between client & server
• no handshaking before sending data
• sender explicitly attaches IP destination address and port #
to each packet
• rcvr extracts sender IP address and port# from received
packet
UDP: transmitted data may be lost or received out-oforder
Application viewpoint:
• UDP provides unreliable transfer
of groups of bytes
(“datagrams") between client and server
10
Client/Server Socket Interaction: UDP
server (running on serverIP)
client
create socket, port= x:
serverSocket =
socket(AF_INET,SOCK_DGRAM)
create socket:
clientSocket =
socket(AF_INET,SOCK_DGRAM)
read datagram from
serverSocket
write reply to
serverSocket
specifying
client address,
port number
Create datagram with server IP and
port=x; send datagram via
clientSocket
read datagram from
clientSocket
close
clientSocket
11
Example App: UDP Client
Python UDPClient
include Python’s socket
library
create UDP socket for
server
get user keyboard
input
Attach server name, port to
message; send into socket
read reply characters from
socket into string
print out received string
and close socket
from socket import *
serverName = ‘hostname’
serverPort = 12000
clientSocket = socket(socket.AF_INET,
socket.SOCK_DGRAM)
message = raw_input(’Input lowercase sentence:’)
clientSocket.sendto(message,(serverName, serverPort))
modifiedMessage, serverAddress =
clientSocket.recvfrom(2048)
print modifiedMessage
clientSocket.close()
12
Example App: UDP Server
Python UDPServer
create UDP socket
bind socket to local port
number 12000
loop forever
Read from UDP socket into
message, getting client’s
address (client IP and port)
send upper case string
back to this client
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('', serverPort))
print “The server is ready to receive”
while 1:
message, clientAddress = serverSocket.recvfrom(2048)
modifiedMessage = message.upper()
serverSocket.sendto(modifiedMessage, clientAddress)
13
Socket Programming with TCP
client must contact server
• server process must first be
running
• server must have created
socket (door) that welcomes
client’s contact
client contacts server by:
• when contacted by client,
server TCP creates new
socket for server process to
communicate
with
that
particular client
– allows server to talk with
multiple clients
– source port numbers used
to distinguish clients (more
in Chap 3)
• Creating
TCP
socket,
specifying IP address, port
number of server process
application viewpoint:
• when client creates socket:
TCP provides reliable, in-order
client
TCP
establishes byte-stream transfer (“pipe”)
connection to server TCP
between client and server
14
Client/Server Socket Interaction: TCP
client
server (running on hostid)
create socket,
port=x, for incoming
request:
serverSocket = socket()
wait for incoming
TCP
connection request
connectionSocket = connection
serverSocket.accept()
read request from
connectionSocket
write reply to
connectionSocket
close
connectionSocket
setup
create socket,
connect to hostid, port=x
clientSocket = socket()
send request using
clientSocket
read reply from
clientSocket
close
clientSocket
15
Example App: TCP Client
Python TCPClient
create TCP socket for
server, remote port 12000
No need to attach server
name, port
from socket import *
serverName = ’servername’
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
sentence = raw_input(‘Input lowercase sentence:’)
clientSocket.send(sentence)
modifiedSentence = clientSocket.recv(1024)
print ‘From Server:’, modifiedSentence
clientSocket.close()
16
Example App: TCP Server
Python TCPServer
create TCP welcoming
socket
server begins listening for
incoming TCP requests
loop forever
server waits on accept()
for incoming requests, new
socket created on return
read bytes from socket (but
not address as in UDP)
close connection to this
client (but not welcoming
socket)
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind((‘’,serverPort))
serverSocket.listen(1)
print ‘The server is ready to receive’
while 1:
connectionSocket, addr = serverSocket.accept()
sentence = connectionSocket.recv(1024)
capitalizedSentence = sentence.upper()
connectionSocket.send(capitalizedSentence)
connectionSocket.close()
17
Example: TCPClient.py and TCPServer.py
TCPClient.py
TCPServer.py
Python 3.5
• print() instead of print
• input() instead of raw_input()
• Explicitly sentence.encode() and modifiedSentence.decode()
Change serverName and serverPort if required
18
Demo: TCPClient and TCPServer
• Start TCPServer.py
in DOS Prompt
> python TCPServer.py
– Use ‘telnet localhost 12000’
to test if it is running
• Press ‘Ctrl-C’ to interrupt
• Start TCPClient.py
from Python IDLE
File → Open: TCPClient.py
Run → Run Module (F5)
Input: Hello, I'm A1025501 from Kaohsiung.
19
Python Documents
The Python Tutorial
https://docs.python.org/3.5/tutorial/
The Python Language Reference
https://docs.python.org/3.5/reference/
The Python Standard Library
https://docs.python.org/3.5/library/
PyPI - the Python Package Index
69060 packages (2015-11-10)
https://pypi.python.org/pypi
1. Introduction
2. Built-in Functions
3. Built-in Constants
4. Built-in Types
5. Built-in Exceptions
6. Text Processing Services
7. Binary Data Services
8. Data Types
9. Numeric and Mathematical Modules
10. Functional Programming Modules
11. File and Directory Access
12. Data Persistence
13. Data Compression and Archiving
14. File Formats
15. Cryptographic Services
16. Generic Operating System Services
17. Concurrent Execution
18. Interprocess Communication and Networking
19. Internet Data Handling
20. Structured Markup Processing Tools
21. Internet Protocols and Support
22. Multimedia Services
23. Internationalization
24. Program Frameworks
25. Graphical User Interfaces with Tk
26. Development Tools
27. Debugging and Profiling
28. Software Packaging and Distribution
29. Python Runtime Services
30. Custom Python Interpreters
31. Importing Modules
32. Python Language Services
33. Miscellaneous Services
34. MS Windows Specific Services
35. Unix Specific Services
36. Superseded Modules
37. Undocumented Modules
20
18. Interprocess Communication and Networking
18.1. socket — Low-level networking interface
18.2. ssl — TLS/SSL wrapper for socket objects
18.3. select — Waiting for I/O completion
18.4. selectors – High-level I/O multiplexing
18.5. asyncio – Asynchronous I/O, event loop, coroutines and tasks
21. Internet Protocols and Support
18.6. asyncore — Asynchronous socket handler
21.1. webbrowser — Convenient Web-browser controller
18.7. asynchat — Asynchronous socket command/response handler
21.2. cgi — Common Gateway Interface support
18.8. signal — Set handlers for asynchronous events
21.3. cgitb — Traceback manager for CGI scripts
18.9. mmap — Memory-mapped file support
21.4. wsgiref — WSGI Utilities and Reference Implementation 19. Internet Data Handling
21.5. urllib — URL handling modules
19.1. email — An email and MIME handling package
21.6. urllib.request — Extensible library for opening URLs
19.2. json — JSON encoder and decoder
21.7. urllib.response — Response classes used by urllib
19.3. mailcap — Mailcap file handling
21.8. urllib.parse — Parse URLs into components
19.4. mailbox — Manipulate mailboxes in various formats
21.9. urllib.error — Exception classes raised by urllib.request 19.5. mimetypes — Map filenames to MIME types
21.10. urllib.robotparser — Parser for robots.txt
19.6. base64 — Base16, Base32, Base64, Base85 Data Encodings
21.11. http — HTTP modules
19.7. binhex — Encode and decode binhex4 files
21.12. http.client — HTTP protocol client
19.8. binascii — Convert between binary and ASCII
21.13. ftplib — FTP protocol client
19.9. quopri — Encode and decode MIME quoted-printable data
21.14. poplib — POP3 protocol client
19.10. uu — Encode and decode uuencode files
21.15. imaplib — IMAP4 protocol client
20. Structured Markup Processing Tools
21.16. nntplib — NNTP protocol client
20.1. html — HyperText Markup Language support
21.17. smtplib — SMTP protocol client
20.2. html.parser — Simple HTML and XHTML parser
21.18. smtpd — SMTP Server
20.3. html.entities — Definitions of HTML general entities
21.19. telnetlib — Telnet client
20.4. XML Processing Modules
21.20. uuid — UUID objects according to RFC 4122
20.5. xml.etree.ElementTree — The ElementTree XML API
21.21. socketserver — A framework for network servers
20.6. xml.dom — The Document Object Model API
21.22. http.server — HTTP servers
20.7. xml.dom.minidom — Minimal DOM implementation
21.23. http.cookies — HTTP state management
20.8. xml.dom.pulldom — Support for building partial DOM trees
21.24. http.cookiejar — Cookie handling for HTTP clients
20.9. xml.sax — Support for SAX2 parsers
21.25. xmlrpc — XMLRPC server and client modules
20.10. xml.sax.handler — Base classes for SAX handlers
21.26. xmlrpc.client — XML-RPC client access
20.11. xml.sax.saxutils — SAX Utilities
21.27. xmlrpc.server — Basic XML-RPC servers
20.12. xml.sax.xmlreader — Interface for XML parsers
21.28. ipaddress — IPv4/IPv6 manipulation library
20.13. xml.parsers.expat — Fast XML parsing using Expat
21
Standard Library
(Internet Related)
Chinese String: big5 v.s. UTF-8 Encodings
Specify the right coding
Python default is utf-8,
but not in all editors
(big5
in
Windows
NotePad)
22