Advertisement
cuteSquirrel

server.py

Mar 26th, 2021 (edited)
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.93 KB | None | 0 0
  1. import socket
  2. import struct
  3. import pickle
  4. import time
  5.  
  6. localhost_with_port_9000 = ( "127.0.0.1", 9000 )
  7.  
  8. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
  9.    
  10.     server_socket.bind( localhost_with_port_9000 )
  11.     server_socket.listen( 1 )
  12.  
  13.     print("\nWaiting for client connection...")
  14.  
  15.     connection, address = server_socket.accept()
  16.  
  17.     with connection as conn:
  18.  
  19.         print(f"connected by {address}")
  20.  
  21.         # 1. Test one data payload dividing into three transmission to client
  22.        
  23.         data_1 = {"message":"hello world. This is test item #1's data"}
  24.         data_1_rawbyte = pickle.dumps(data_1)
  25.         id = 1
  26.  
  27.         payload = data_1_rawbyte
  28.  
  29.         # definition of header
  30.         # first field is id
  31.         # second field is size of payload
  32.         header = (id, len(payload) )
  33.  
  34.         # define header size as 2 * 4 Unsigne Int = 8 Bytes
  35.         header_rawbyte_1 = struct.pack("!2I", *header)
  36.  
  37.         # divide one data payload into three transmission
  38.         send_1_full = header_rawbyte_1 + payload
  39.         send_1_a = send_1_full[:5]
  40.         send_1_b = send_1_full[5:10]
  41.         send_1_c = send_1_full[10:]
  42.  
  43.         # send 1_a to clinet
  44.         print("\n send data 1_a to clinet ... ")
  45.         connection.sendall( send_1_a )
  46.  
  47.         time.sleep(0.5)
  48.  
  49.         # send 1_b to clinet
  50.         print("\n send data 1_b to clinet ... ")
  51.         connection.sendall( send_1_b )
  52.  
  53.         time.sleep(0.5)
  54.  
  55.         # send 1_c to clinet
  56.         print("\n send data 1_c to clinet ... ")
  57.         connection.sendall( send_1_c )
  58.  
  59.         time.sleep(0.5)
  60.  
  61.  
  62.         # ---------------------------------------------------------
  63.  
  64.  
  65.         ## 2. Test merging two differnt data payload divide into one transmission to client
  66.  
  67.         data_2 = {"message":"Meow meow. This is test item #2's data"}
  68.         data_2_rawbyte = pickle.dumps(data_2)
  69.         id = 2
  70.  
  71.         payload = data_2_rawbyte
  72.  
  73.         # definition of header
  74.         # first field is id
  75.         # second field is size of payload
  76.         header = (id, len(payload) )
  77.  
  78.         # define header size as 2 * 4 Unsigne Int = 8 Bytes
  79.         header_rawbyte_2 = struct.pack("!2I", *header)
  80.  
  81.  
  82.  
  83.         data_3 = {"message":"Meow meow. This is test item #3's data", "demand":"quit connection"}
  84.         data_3_rawbyte = pickle.dumps(data_3)
  85.         id = 3
  86.  
  87.         payload = data_3_rawbyte
  88.  
  89.         # definition of header
  90.         # first field is id
  91.         # second field is size of payload
  92.         header = (id, len(payload) )
  93.  
  94.         # define header size as 2 * 4 Unsigne Int = 8 Bytes
  95.         header_rawbyte_3 = struct.pack("!2I", *header)
  96.  
  97.         send_2_and_3 = header_rawbyte_2 + data_2_rawbyte + header_rawbyte_3 + data_3_rawbyte
  98.  
  99.         # send 2 and 3 to clinet
  100.         print("\n send data 2 and data 3 in one transmission to clinet ... ")
  101.         connection.sendall( send_2_and_3 )
  102.  
  103.         time.sleep(0.5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement