0% found this document useful (0 votes)
6 views1 page

Python Socket Chat Example

Uploaded by

mobsmokieop
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)
6 views1 page

Python Socket Chat Example

Uploaded by

mobsmokieop
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

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.

You might also like