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

PYTHON EXP 7 (Pujan)

The document outlines a laboratory experiment for a Python programming course focused on creating a TCP/IP server and client for message exchange. It includes prerequisites, expected outcomes, and a detailed procedure with example server and client code. The experiment aims to help students understand socket programming concepts in Python.

Uploaded by

exxo.711
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)
16 views3 pages

PYTHON EXP 7 (Pujan)

The document outlines a laboratory experiment for a Python programming course focused on creating a TCP/IP server and client for message exchange. It includes prerequisites, expected outcomes, and a detailed procedure with example server and client code. The experiment aims to help students understand socket programming concepts in Python.

Uploaded by

exxo.711
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/ 3

Sub: Skill Base Lab: Python Programming Course code: CSL405

Semester: IV Course: AI&DS

Laboratory no: 101 Name of subject teacher: Prof. Rajesh Khotre

Name: Aryan Fursule Roll no: VU2F2324032

Experiment No.07
A.1 Aim:

To demonstrate of simple socket for basic information exchange between server and client.

Create a TCP/IP server program to send messages to client and client program to receive messages
from the server.

A.2 Prerequisite:

1. C, JAVA Language

A.3 Outcome:

After successful completion of this experiment students will be able to

To demonstrate concepts in python

A.4 Theory& Procedure:

Sockets are the endpoints of a bidirectional communications channel. Sockets may communicate
within a process, between processes on the same machine, or between processes on different
continents.

Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP,
UDP, and so on. The socket library provides specific classes for handling the common transports as
well as a generic interface for handling the rest

PROGRAM:
SERVER CODE:

import socket

HOST = '127.0.0.1' # Localhost


PORT = 12345

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server_socket.bind((HOST, PORT))

server_socket.listen(1)

print(f"Server is listening on {HOST}:{PORT}...")

conn, addr = server_socket.accept()

print(f"Connected by {addr}")

messages = ["Hello, Client!", "How are you?", "This is a basic TCP/IP socket example.", "Goodbye!"]

for message in messages:

conn.sendall(message.encode())

print(f"Sent: {message}")

conn.close()

server_socket.close()

CLIENT CODE:

import socket

HOST = '127.0.0.1'

PORT = 12345

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

client_socket.connect((HOST, PORT))

print(f"Connected to server at {HOST}:{PORT}")


while True:

data = client_socket.recv(1024).decode()

if not data:

break

print(f"Received: {data}")

client_socket.close()

OUTPUT:
SERVER TERMINAL OUTPUT:

Server is listening on 127.0.0.1:12345...

Connected by ('127.0.0.1', 54321)

Sent: Hello, Client!

Sent: How are you?

Sent: This is a basic TCP/IP socket example.

Sent: Goodbye!

CLIENT TERMINAL OUTPUT:

Connected to server at 127.0.0.1:12345

Received: Hello, Client!

Received: How are you?

Received: This is a basic TCP/IP socket example.

Received: Goodbye!

You might also like