#Server
import socket
import json
dict = {
"person1" : {
"Name" : "Sarah Smith",
"DOB" : '2/5/2003',
"Address" : '321 Python Way',
"Medication" : ["TRE***", "AML***"]
},
"person2" : {
"Name" : "John Doe",
"DOB" : '5/17/1980',
"Address" : '3564 Beverly Hill Rd.',
"Medication" : []
},
"person3" : {
"Name" : "Kimberly Shaw",
"DOB" : '9/10/1970',
"Address" : '1245 Washington Tree Terrace',
"Medication" : ["DOX****"]
},
"person4" : {
"Name" : "Amy Lee",
"DOB" : '4/3/1997',
"Address" : '943 Beach Front Ave.',
"Medication" : ["PHE****"]
},
"person5" : {
"Name" : "John Doe",
"DOB" : '5/17/1980',
"Address" : '5567 Apple Farm Blvd.',
"Medication" : ['ART***', 'LOW***']
},
"person6" : {
"Name" : "Amy Lee",
"DOB" : '12/13/2000',
"Address" : '6324 Sunnydale Drive',
"Medication" : ["COL****", 'NIM****']
},
"person7" : {
"Name" : "Harold Johnson",
"DOB" : '6/9/1945',
"Address" : '7845 Senior Citizen Ave.',
"Medication" : []
},
}
def run_server():
# create a socket object
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_ip = "127.0.0.1"
port = 8000
# bind the socket to a specific address and port
server.bind((server_ip, port))
# listen for incoming connections
server.listen(0)
print(f"Listening on {server_ip}:{port}")
# accept incoming connections
client_socket, client_address = server.accept()
print(f"Accepted connection from {client_address[0]}:{client_address[1]}")
# receive data from the client
while True:
request = client_socket.recv(1024)
request = request.decode("utf-8") # convert bytes to string
# if we receive "close" from the client, then we break
# out of the loop and close the conneciton
if request.lower() == "close":
# send response to the client which acknowledges that the
# connection should be closed and break out of the loop
client_socket.send("closed".encode("utf-8"))
break
if request[:4] == 'Name':
name_list = [names['Name'] for names in dict.values() if names['Name'] == request[5:]]
print(name_list)
if len(name_list) == 0:
response = 'N/A'
print(response)
client_socket.send(response.encode('utf-8'))
else:
# response = "accepted".encode("utf-8") # convert string to bytes
response = '\n'.join(name_list)
# convert and send accept response to the client
client_socket.send(response.encode('utf-8'))
elif request[:3] == 'DOB':
dob_list = [dob['DOB'] for dob in dict.values() if dob['DOB'] == request[4:]]
print(dob_list)
if len(dob_list) == 0:
response = 'N/A'
print(response)
client_socket.send(response.encode('utf-8'))
else:
# response = "accepted".encode("utf-8") # convert string to bytes
response = '\n'.join(dob_list)
# convert and send accept response to the client
client_socket.send(response.encode('utf-8'))
elif request[:7] == 'Address':
address_list = [address['Address'] for address in dict.values() if address['Address'] == request[8:]]
print(address_list)
if len(address_list) == 0:
response = 'N/A'
print(response)
client_socket.send(response.encode('utf-8'))
else:
# response = "accepted".encode("utf-8") # convert string to bytes
response = '\n'.join(address_list)
# convert and send accept response to the client
client_socket.send(response.encode('utf-8'))
else:
medication_available = []
if request[4:11] == 'Address':
dictionary_key = [key for key, value in dict.items() if value.get('Address') == request[12:]]
print(dictionary_key)
print(dict[dictionary_key[0]]['Medication'])
medication_available.append(dict[dictionary_key[0]]['Medication'])
if len(medication_available[0]) == 0:
response = 'N/A'
print(response)
client_socket.send(response.encode('utf-8'))
else:
# response = "accepted".encode("utf-8") # convert string to bytes
response = '\n'.join(medication_available[0])
# convert and send accept response to the client
client_socket.send(response.encode('utf-8'))
else:
medication_available = []
dictionary_key = [key for key, value in dict.items() if value.get('DOB') == request[8:]]
print(dictionary_key)
print(dict[dictionary_key[0]]['Medication'])
medication_available.append(dict[dictionary_key[0]]['Medication'])
if len(medication_available[0]) == 0:
response = 'N/A'
print(response)
client_socket.send(response.encode('utf-8'))
else:
# response = "accepted".encode("utf-8") # convert string to bytes
response = '\n'.join(medication_available[0])
# convert and send accept response to the client
client_socket.send(response.encode('utf-8'))
print(f"Received: {request}")
# close connection socket with the client
client_socket.close()
print("Connection to client closed")
# close server socket
server.close()
run_server()
#Client
import socket
def close_connection(client):
msg = 'close'
client.send(msg.encode("utf-8")[:1024])
def get_meds(client, msg):
meds = 'Meds'
msg = f'{meds}{msg}'
client.send(msg.encode("utf-8")[:1024])
def run_client():
# create a socket object
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_ip = "127.0.0.1" # replace with the server's IP address
server_port = 8000 # replace with the server's port number
# establish connection with server
client.connect((server_ip, server_port))
while True:
# input message and send it to the server
wanted_info = 'Name'
msg = input("First and Last Name Please: ")
msg = f'{wanted_info} {msg}'
client.send(msg.encode("utf-8")[:1024])
# receive message from the server
response = client.recv(1024)
response = response.decode("utf-8")
# if server sent us "closed" in the payload, we break out of the loop and close our socket
if response.lower() == "closed":
break
if response == 'N/A':
print('Sorry you are not on record here. Have a good day.')
close_connection(client)
break
else:
wanted_info = 'DOB'
msg = input("Can I please have your DOB? ")
msg = f'{wanted_info} {msg}'
client.send(msg.encode("utf-8")[:1024])
response = client.recv(1024)
response = response.decode("utf-8")
if response == 'N/A':
msg = input("Can you please try again that DOB doesn't exist? ")
msg = f'{wanted_info} {msg}'
client.send(msg.encode("utf-8")[:1024])
response = client.recv(1024)
response = response.decode("utf-8")
if response == 'N/A':
print('Sorry you are not on record here. Have a good day.')
close_connection(client)
break
else:
get_meds(client, msg)
# receive message from the server
response = client.recv(1024)
response = response.decode("utf-8")
if response == 'N/A':
print('You don\'t have any prescriptions available today')
else:
print(f'You have these medications ready today: {response}. Have a good day!')
close_connection(client)
break
elif response.count('/') > 2:
wanted_info = 'Address'
msg = input('Can I please have your address?')
msg = f'{wanted_info} {msg}'
client.send(msg.encode("utf-8")[:1024])
# receive message from the server
response = client.recv(1024)
response = response.decode("utf-8")
if response == 'N/A':
print('Sorry you are not on record here. Have a good day.')
close_connection(client)
break
else:
get_meds(client, msg)
# receive message from the server
response = client.recv(1024)
response = response.decode("utf-8")
if response == 'N/A':
print('You don\'t have any prescriptions available today')
else:
print(f'You have these medications ready today: {response}. Have a good day!')
close_connection(client)
break
else:
get_meds(client, msg)
# receive message from the server
response = client.recv(1024)
response = response.decode("utf-8")
if response == 'N/A':
print('You don\'t have any prescriptions available today')
else:
print(f'You have these medications ready today: {response}. Have a good day!')
close_connection(client)
break
# close client socket (connection to the server)
client.close()
print("Connection to server closed")
run_client()