NP Section 2
NP Section 2
Socket Module
SOCK_STREAM SOCK_DGRAM
For TCP protocols For UDP protocols
Reliable delivery Unrelible delivery
Guaranteed correct ordering
No order guaranteed
of packets
Connection-oriented No notion of connection(UDP)
Bidirectional Not Bidirectional
gethostname() :
Function Signature: socket.gethostname()
import socket
hostname=socket.gethostname()
print(hostname)
import socket
if __name__ == '__main__':
hostname = 'www.python.org'
addr = socket.gethostbyname(hostname)
port=socket.getservbyname('domain')
print (port)
serv=socket.getservbyport(53)
print(serv)
Method Description
The bind() method of Python's socket class assigns an IP address and a port
number to a socket instance.
The bind() method is used when a socket needs to be made a server socket.
As server programs listen on published ports, it is required that a port and
the IP address to be assigned explicitly to a server socket.
For client programs, it is not required to bind the socket explicitly to a port.
The kernel of the operating system takes care of assigning the source IP and
a temporary port number.
Example
s.bind(("127.0.0.1", 32007));
The recvfrom() method Python's socket class, reads a number of bytes sent from
an UDP socket.
The recvfrom() method can be used with an UDP server to receive data from a
UDP client or it can be used with an UDP client to receive data from a UDP server.
Method Signature/Syntax:
socket.recvfrom(bufsize[, flags])
Parameters:
bufsize - The number of bytes to be read from the UDP socket.
flags - This is an optional parameter. As supported by the operating system.
Multiple values combined together using bitwise OR. The default value is zero.
Return Value:
Returns a bytes object read from an UDP socket and the address of the client socket as a
tuple.
The method sendto() of the Python's socket class, is used to send datagrams to a UDP socket.
The communication could be from either side. It could be from client to server or from the
server to client.
Method Signature/Syntax:
sendto(bytes, flags, address)
Parameters:
bytes - The data to be sent in bytes format. If the data is in string format, str.encode() method
can be used to convert the strings to bytes.
flags - As supported by the operating system, multiple values can be combined using bitwise
OR. This optional parameter has a default value of 0.
address - A tuple consisting of IP address and port number.
Return Value:
Returns the number of bytes sent.
import socket
#Create Socket object , SOCK_DGRAM for UDP
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
#get the local machine name
host=socket.gethostname()
port=12345
server_add=(host,port)
msg='hello my first udp program'
#convert string message to bytes
msg=msg.encode('ascii')
#send the message to server
sock.sendto(msg,server_add)
#recieve message from the server, recvfrom return message and sender address
data,add= sock.recvfrom(1024)
#convert the recived bytes message to string
text = data.decode('ascii')
print('recive data from',add,'data=',text)
Created By Shreen Khalaf 3/1/2022
Encoding and Decoding
Decoding is what happens when bytes are on their way into your application
and you need to figure out what they mean.
Encoding is the process of taking character strings that you are ready to
present to the outside world and turning
them into bytes
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
import socket
def client(ip,port):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
text = 'Broadcast datagram!'
sock.sendto(text.encode('ascii'), (ip, port))
msg, add = sock.recvfrom(1024)
msg = msg.decode('ascii')
print(add,msg)
client("<broadcast>",12345)