Simple Client-Server Chat using Python Sockets
This document provides simple Python code for a continuous chatting loop between a server and a
client using socket programming. Both can send and receive messages until one side types 'exit' to
quit.
Server Code ([Link])
import socket
s = [Link]()
[Link](("[Link]", 12345))
[Link](1)
print("Server waiting for connection...")
conn, addr = [Link]()
print("Connected with", addr)
while True:
data = [Link](1024).decode()
if [Link]() == "exit":
print("Client left the chat.")
break
print("Client:", data)
msg = input("You: ")
[Link]([Link]())
if [Link]() == "exit":
break
[Link]()
[Link]()
Client Code ([Link])
import socket
s = [Link]()
[Link](("[Link]", 12345))
print("Connected to server. Type 'exit' to quit.")
while True:
msg = input("You: ")
[Link]([Link]())
if [Link]() == "exit":
break
data = [Link](1024).decode()
if [Link]() == "exit":
print("Server ended the chat.")
break
print("Server:", data)
[Link]()
Run Instructions: 1. Open two terminals. 2. In the first terminal, run: python3 [Link] 3. In the
second terminal, run: python3 [Link] 4. Chat back and forth. Type 'exit' to quit.