0% found this document useful (0 votes)
29 views4 pages

CN EXPT 5-1

This document contains code for implementing socket programming in Python. It includes code for a TCP client and server that connects to Google, as well as a UDP client and server. The TCP client code gets the IP address of Google.com and connects to it on port 80. The UDP server code binds a socket to port 12345, puts it in listening mode, and accepts connections, sending a message to clients and closing the connection. The UDP client code connects to this server and receives the message. The conclusion states the goal of implementing socket programming in Python was achieved.

Uploaded by

Gaurang Mapuskar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views4 pages

CN EXPT 5-1

This document contains code for implementing socket programming in Python. It includes code for a TCP client and server that connects to Google, as well as a UDP client and server. The TCP client code gets the IP address of Google.com and connects to it on port 80. The UDP server code binds a socket to port 12345, puts it in listening mode, and accepts connections, sending a message to clients and closing the connection. The UDP client code connects to this server and receives the message. The conclusion states the goal of implementing socket programming in Python was achieved.

Uploaded by

Gaurang Mapuskar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: Sanchit Mhatre

Div: D10B Roll no.:34

PRACTICAL 5
AIM : To study and Implement Socket Programming using PYTHON.
1. TCP Client, TCP Server

2. UDP Client, UDP Server

CODE:
Program 1.
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
import socket
ip = socket.gethostbyname('www.google.com')
print(ip)

Output:

Program 2.
# An example script to connect to Google using socket programming in
Python
import socket # for socket
import sys

try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ("Socket successfully created")
except socket.error as err:
print ("socket creation failed with error %s" %(err))

# default port for socket


port = 80

try:
host_ip = socket.gethostbyname('www.google.com')
except socket.gaierror:
# this means could not resolve the host
print ("there was an error resolving the host")
sys.exit()

# connecting to the server


s.connect((host_ip, port))

print ("the socket has successfully connected to google")

Output:

Program 3.
# first of all import the socket library
import socket

# next create a socket object


s = socket.socket()
print ("Socket successfully created")

# reserve a port on your computer in our


# case it is 12345 but it can be anything
port = 12345

# Next bind to the port


# we have not typed any ip in the ip field
# instead we have inputted an empty string
# this makes the server listen to requests
# coming from other computers on the network
s.bind(('', port))
print ("socket binded to %s" %(port))

# put the socket into listening mode


s.listen(5)
print ("socket is listening")

# a forever loop until we interrupt it or


# an error occurs
while True:

# Establish connection with client.


c, addr = s.accept()
print ('Got connection from', addr )

# send a thank you message to the client. encoding to send byte type.
c.send('Thank you for connecting'.encode())

# Close the connection with the client


c.close()

# Breaking once connection closed


break

Output:

Program 4.
# Import socket module
import socket

# Create a socket object


s = socket.socket()

# Define the port on which you want to connect


port = 12345

# connect to the server on local computer


s.connect(('127.0.0.1', port))

# receive data from the server and decoding to get the string.
print (s.recv(1024).decode())
# close the connection
s.close()
CONCLUSION: Therefore, we have successfully implemented socket programming using
python and understood how to successfully create sockets.

You might also like