Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

udp client server chat program in python

import os
from pyfiglet import Figlet
os.system("clear")
pyf = Figlet(font='puffy')
a = pyf.renderText("UDP Chat App with Multi-Threading")
os.system("tput setaf 3")
print(a)
# importing modules for the chat app
import socket
import time
import threading
import sys
# AF_INET = Network Address Family : IPv4
# SOCK_DGRAM = DataGram Socket : UDP
# Function for receiving
def receiver():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind((ip_sender, port_sender)) #binding the IP address and port number
    while True:
        msg = s.recvfrom(1024)
        print("
"+msg[0].decode())
        if "exit" in msg[0].decode() or "bye" in msg[0].decode():
            sys.exit()
#Function for sending
def sender():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    text = "hello"
    while True:
        if "bye" in text or "exit" in text or "finish" in text:
            exit()
        else:
            text = input(f'{name}:')
            text = name+":"+text
            s.sendto(text.encode(), (ip_receiver, port_receiver))
print("Initializing....")
ip_receiver = input("
Enter the IP of reciever: ")
port_receiver = int(input("
Enter the port of the reciever: "))
ip_sender = input("
Enter the IP of your system : ")
port_sender = int(input("
Enter the port of your system: "))
name = input("Enter your name: ")
print("Waiting for client....")
time.sleep(1)
print("Connection established....")
# Using Multi-threading
send = threading.Thread(target=sender)
receive = threading.Thread(target=receiver)
send.start()
receive.start()
Comment

PREVIOUS NEXT
Code Example
Python :: daemon in os 
Python :: password validation in python 
Python :: adding if statements and for loop in pyhton with tuple 
Python :: groupby sum and mean 2 columns 
Python :: extract parameter of voice using python 
Python :: mkvirtualenv 
Python :: python code to encrypt and decrypt a stringn with password 
Python :: save csv with today date pandas 
Python :: how to get random images frrom quotefancy python 
Python :: django compile database 
Python :: channel unlock command in discord.py 
Python :: mostFrequentDays python 
Python :: get top feature gridsearchcv 
Python :: variable types in python 
Python :: Filter by len() 
Python :: dbscan clustering of latitudes and longitudes 
Python :: geopandas change dtype of a columns 
Python :: python forward and bachward seperators 
Python :: add a third dimension matrix dataset python 
Python :: pandas show head and tail 
Python :: doc2text python example 
Python :: mudopy 
Python :: numpy bitwise_or multiple images 
Python :: python turn seconds into zulu time 
Python :: fibonacci series recursive python 
Python :: pylatex subsection 
Python :: how to do downsampling in python 
Python :: Python docx title 
Python :: k-means clustering and disabling clusters 
Python :: pandas get only entries that match list 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =