Advertisement
VssA

JSON_socket

Dec 12th, 2023
761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.94 KB | None | 0 0
  1. import socket
  2. import json
  3. import pandas as pd
  4.  
  5. def get_website(organization_name):
  6.     data = pd.read_csv('organizations.csv')
  7.     website = data[data['Name'] == organization_name]['Website'].values
  8.     if len(website) > 0:
  9.         return website[0]
  10.     else:
  11.         return "Organization not found"
  12.  
  13. def get_country(organization_name):
  14.     data = pd.read_csv('organizations.csv')
  15.     country = data[data['Name'] == organization_name]['Country'].values
  16.     if len(country) > 0:
  17.         return country[0]
  18.     else:
  19.         return "Organization not found"
  20.  
  21. def get_number_of_employees(organization_name):
  22.     data = pd.read_csv('organizations.csv')
  23.     employees = data[data['Name'] == organization_name]['Number of Employees'].values
  24.     if len(employees) > 0:
  25.         return str(employees[0])
  26.     else:
  27.         return "Organization not found"
  28.  
  29. def start_server():
  30.     host = "127.0.0.32"
  31.     port = 12345
  32.    
  33.     server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  34.     server_socket.bind((host, port))
  35.     server_socket.listen(1)
  36.  
  37.     while True:
  38.         client_socket, address = server_socket.accept()
  39.         data = client_socket.recv(1024)
  40.         if data is None:
  41.             break
  42.         message = data.decode()
  43.         if message == "exit":
  44.             break
  45.         print(f'Строка от клиента: {message}')
  46.         request_data = json.loads(message)
  47.        
  48.         operation = request_data.get("operation")
  49.         name = request_data.get("name")
  50.        
  51.         if operation == "get_website":
  52.             result = get_website(name)
  53.         elif operation == "get_country":
  54.             result = get_country(name)
  55.         elif operation == "get_number_of_employees":
  56.             result = get_number_of_employees(name)
  57.         else:
  58.             result = "Invalid operation"
  59.        
  60.         client_socket.send(result.encode())
  61.         client_socket.close()
  62.  
  63. if __name__ == "__main__":
  64.     start_server()
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement