[go: up one dir, main page]

0% found this document useful (0 votes)
21 views3 pages

CN Week 4

The document provides code examples for socket programming in Python including getting the IP address of a system, implementing an echo server and client, building a basic chat application with a client-server model using TCP, and creating an authenticated client-server application where the client must enter a password to communicate with the server.

Uploaded by

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

CN Week 4

The document provides code examples for socket programming in Python including getting the IP address of a system, implementing an echo server and client, building a basic chat application with a client-server model using TCP, and creating an authenticated client-server application where the client must enter a password to communicate with the server.

Uploaded by

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

Assignment - 04

1) Write a program to find the IP address of the system.

import socket
print("Host Name:", socket.gethostname(), "\nIp
Address:", socket.gethostbyname(socket.gethostname()))
2) Write a socket program for implementation of echo.

Server side code:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1", 9090))
s.listen()
(c, cip) = s.accept()
c.send(c.recv(1024))
s.close()
Client side code:

import socket
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c.connect(("127.0.0.1", 9090))
data = input()
c.send(data.encode())
dataFromServer = c.recv(1024)
print(dataFromServer.decode())
c.close()
3) Write a client-server application for chat using TCP.

Server side code:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1", 9090))
s.listen()
while True:
(c, cip) = s.accept()
data = c.recv(1024).decode()
print("Client:",data)
data = input("Enter Text: ")
c.send(data.encode())
Client side code:

import socket
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c.connect(("127.0.0.1", 9090))
while True:
data = input("Enter Text: ")
c.send(data.encode())
data = c.recv(1024).decode()
print("Server:",data)
4) Write a program using client server socket programming: Client needs to authenticate itself by entering a
server defined string as a password (like OTP) and then to say Hi to server. Server replies with a Hello. Press
any key to exit.

Server side code:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1", 9090))
s.listen()
(c, cip) = s.accept()
c.send("Enter OTP:".encode())
otp = c.recv(1024).decode()
if otp == '8894':
c.send("You are Authenticated".encode())
data = c.recv(1024).decode()
print("Client:",data)
data = input("Enter Text: ")
c.send(data.encode())
else:
c.send("You are Authenticated".encode())
s.close()
Client side code:

import socket
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c.connect(("127.0.0.1", 9090))
data = c.recv(1024).decode()
print(data, end=" ")
otp = input()
c.send(otp.encode())
data = c.recv(1024).decode()
print(data)
if data == "You are Authenticated":
data = input("Enter Text: ")
c.send(data.encode())
data = c.recv(1024).decode()
print("Server:",data)
else:
c.close()

You might also like