SHARE
TWEET

Untitled

a guest Oct 14th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import socket
  2. import pickle
  3. import datetime
  4.  
  5. HOST = '127.0.0.1'
  6. PORT = 65432
  7.  
  8. class Client:
  9.     def __init__(self, _host, _port):
  10.         self.host = _host
  11.         self.port = _port
  12.         self.con = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  13.  
  14.     def connect(self):
  15.         self.con.connect((self.host, self.port))
  16.  
  17.     def send(self, data):
  18.         data_serialized = pickle.dumps(data)
  19.         self.con.send(data_serialized)
  20.  
  21.     def recieve(self):
  22.         data = self.con.recv(4096)
  23.         return pickle.loads(data)
  24.    
  25.     def close(self):
  26.         self.con.close()
  27.        
  28.  
  29.  
  30. try:
  31.     client = Client(HOST, PORT)
  32.     client.connect()
  33.  
  34.     out_data = {"msg_from_client": "Hello from Client!", "sent_on": datetime.datetime.now()}    
  35.     client.send(out_data)
  36.    
  37.     in_data = client.recieve()
  38.     client_id = in_data["client_id"]
  39.     server_msg = in_data["msg_from_server"]
  40.    
  41.     print("Client " + str(client_id) + " successfully connected to server")
  42.     print("Server says: " + server_msg)
  43.  
  44. except socket.error as socket_exception:
  45.     print(socket_exception)
  46.  
  47. # client.close()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top