Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
tcp-client--server-pyhton/tcp_server.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
230 lines (199 sloc)
6.62 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import threading | |
#setting the header | |
head = 64 | |
#setting port number | |
port = 5050 | |
#getting the ip from computer | |
server_ip = socket.gethostbyname(socket.gethostname()) | |
#bundling server name and port number | |
sever_address = (server_ip, port) | |
#setting msg type as uf-8 | |
msg_type = 'utf-8' | |
#value for disconnection msg | |
disconnet = "Connection Disconneted" | |
#declaring socket connetion for the server | |
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
server.bind(sever_address) | |
class Saturday(): | |
def __init__(self): | |
self.adult=10 | |
self.child=10 | |
class VIP(): | |
def __init__(self): | |
self.adult=10 | |
self.child=10 | |
class Sunday(): | |
def __init__(self): | |
self.adult=10 | |
self.child=10 | |
class Weekend(): | |
def __init__(self): | |
self.adult=10 | |
self.child=10 | |
#funtion calculation of ticket cost | |
def ticket_cost(ticket_type,x,y): | |
waiting_adult=[] | |
waiting_child=[] | |
#checking for type as saturday | |
if(ticket_type=="Saturday"): | |
t1=Saturday() | |
if t1.adult>x: | |
tot_adult=x*25 | |
t1.adult=t1.adult-x | |
if t1.child>y: | |
tot_child=y*20 | |
t1.child=t1.child-y | |
tot=tot_adult+tot_child | |
# #checking for type as VIP | |
elif(ticket_type=="VIP"): | |
t2=VIP() | |
if t2.adult>x: | |
tot_adult=x*50 | |
t2.adult=t2.adult-x | |
if t2.child>y: | |
tot_child=y*25 | |
t2.child=t2.child-y | |
tot=tot_adult+tot_child | |
#checking for type as Sunday | |
elif(ticket_type=="Sunday"): | |
t4=Sunday() | |
if t4.adult>x: | |
tot_adult=x*10 | |
t4.adult=t4.adult-x | |
if t4.child>y: | |
tot_child=y*7.5 | |
t4.child=t4.child-y | |
tot=tot_adult+tot_child | |
#checking for type Weekend | |
elif(ticket_type=="Weekend"): | |
t3=Weekend() | |
if t3.adult>x: | |
tot_adult=x*30 | |
t3.adult=t3.adult-x | |
if t3.child>y: | |
tot_child=y*22 | |
t3.child=t3.child-y | |
tot=tot_adult+tot_child | |
else: | |
waiting_adult.append(x) | |
waiting_child.append(y) | |
#returning the total value | |
return tot | |
#ticket for reservating the | |
def booking_reservation(msg): | |
msg_list=msg.split() | |
#intialing a list called reservation | |
reservations=[] | |
i=1 | |
#loop for itertating through items in the list | |
while i<len(msg_list): | |
#adding reservation to variable | |
reservation={'Name':msg_list[0], | |
'Type_of_Ticket':msg_list[i], | |
'Adult_tickets':msg_list[i+1], | |
'Child_tickets':msg_list[i+2]} | |
reservations.append(reservation) | |
i=i*4 | |
x=0 | |
d=0 | |
#intialising total ticket cost as zero | |
total_ticket_cost=0 | |
#caculating total ticket cost for all values | |
for reservation in reservations: | |
ticket_type=reservation["Type_of_Ticket"] | |
x=int(reservation["Adult_tickets"]) | |
y=int(reservation["Child_tickets"]) | |
n=ticket_cost(ticket_type,x,y) | |
total_ticket_cost=total_ticket_cost+n | |
#checking ticket cost for all values grater than 500 the 10% discount given | |
if total_ticket_cost>500: | |
total_ticket_cost=total_ticket_cost-total_ticket_cost*.10 | |
d=1 | |
#value is returned | |
return total_ticket_cost,d | |
# functions for booking class | |
def class_booking_cost(class_type,x,y): | |
#calculating for baking | |
if class_type=="Baking": | |
adult_tot=x*8 | |
child_tot=y*5 | |
tot=adult_tot+child_tot | |
#calculating for Dancing class | |
if class_type=="Dancing_Class": | |
adult_tot=x*15 | |
child_tot=y*10 | |
tot=adult_tot+child_tot | |
#calculating for Crafts | |
if class_type=="Craft": | |
adult_tot=x*10 | |
child_tot=y*7.55 | |
tot=adult_tot+child_tot | |
#calculating for Disco | |
if class_type=="Disco": | |
adult_tot=x*15 | |
child_tot=y*11 | |
tot=adult_tot+child_tot | |
return tot | |
#funtion for cooking class | |
def class_booking(msg): | |
msg_list=msg.split() | |
class_reservation={'Type_of_class':msg_list[1], | |
'Adult_tickets':msg_list[2] , | |
'Child_tickets':msg_list[3] } | |
class_type=class_reservation['Type_of_class'] | |
x=int(class_reservation['Adult_tickets']) | |
y=int(class_reservation['Child_tickets']) | |
#calcluting cost of cooking class | |
class_cost=class_booking_cost(class_type,x,y) | |
return class_cost | |
#funtion for handling the client message | |
def client_msg_handle(conn, addr): | |
print(f" connection address: %s{addr} conected.") | |
#delcaring connetion value to be true | |
connection_val = True | |
#intialing loop checking msg length and message | |
while connection_val: | |
msglen = conn.recv(head).decode(msg_type) | |
#checking for message is empty | |
if msglen: | |
class_cost = 0 | |
msglen = int(msglen) | |
#recving message | |
msg = conn.recv(msglen).decode(msg_type) | |
msg_list=msg.split() | |
#condition for disconecting msg | |
if msg == disconnet: | |
connection_val = False | |
#checking for class | |
if msg =="n": | |
continue | |
elif msg_list[0] == "y": | |
class_cost=class_booking(msg) | |
else: | |
cost,dis=booking_reservation(msg) | |
if dis==1: | |
conn.send(f" \n\nYour Tickets for theRobotics Festival are booked and cost {cost} 10 perecent discount given ".encode(msg_type)) | |
else: | |
conn.send(f" \n\nYour Tickets for theRobotics Festival are booked and cost {cost} No discount given ".encode(msg_type)) | |
total_cost=class_cost+cost | |
print(total_cost) | |
conn.send(f" \n\nYour Tickets for theRobotics Festival are booked and cost {total_cost} No discount given ".encode(msg_type)) | |
conn.send("\n\n received by server".encode(msg_type)) | |
#closing connection | |
conn.close() | |
#funtion for stating the sever | |
def start(): | |
#checking for message | |
server.listen() | |
print(f" Server is listening on {server_ip}") | |
#loop for intialing no of connections | |
while True: | |
#establing new connetion to server | |
conn, addr = server.accept() | |
thread = threading.Thread(target=client_msg_handle, args=(conn, addr)) | |
thread.start() | |
#printing the number of connections | |
print(f"no of connections are {threading.activeCount() - 1}") | |
print(" server is starting...") | |
start() |