Documente Academic
Documente Profesional
Documente Cultură
Python Advanced #5
Sockets
Sockets are the programming abstractions for
connections.
They allow us to communicate in a bidirectional
manner.
Once they are connected or ready to transmit.
We can use them to send data and receive data.
They implement the common transport
protocols TCP and UDP.
Socket Methods
socket(socket_family, socket_type)
The constructer creates a new socket.
bind((hostname,port))
Bind takes a turple of a host address and port
listen()
Starts listening for TCP connections
accept()
Accepts a connection when found .(returns new
socket)
Socket Methods
connect((hostname,port))
Takes a turple of the address and port.
recv(buffer)
Tries to grab data from a TCP connection.
(Waits)
send(bytes)
attempts to send the bytes given to it.
close()
Closes a socket/connection and frees the port.
TCP
Transmission Control Protocol.
Reliable Connection based Protocol
Ordered & Error checked (simple
checksum)
Used by Web Browsers, Email, SSH, FTP,
etc
Capitalize Sentence
Program
UDP
User Datagram Protocol
Un-Reliable Connection-less based
protocol
Low Overhead
Used for VOIP, Online Games, Streaming.
UDP Capitalize
Lets create the same program as before this
time using a UDP connection.
udpServer.py and udpClient.py
You will notice we use recvfrom() and sendto()
This is because UDP is Connectionless, we
need to tell the data where to go when we
send it.
Non Blocking
It is possible and often needed, to set a socket
to non blocking. This means that the program
will not sit and wait for data to be received.
Often used in Real-time programs.
When a task cannot be spread over threads.
Always throws an error if there is nothing in
the buffer!!!
Next
CGI Programming
Feel free to leave any questions in
the comments.