ConnectionTest2004.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """
  2. echo "执行:进入调试" \
  3. && project_path="/home/sri/repositories/repositories/sri-project.demo-py/sri-dino-pyserver01" \
  4. && cd ${project_path} \
  5. && sudo docker exec -it sri-dino-pyserver01 bash
  6. python3 /home/sri/repositories/repositories/sri-project.demo-py/sri-dino-pyserver01/lib/ConnectionTest.py
  7. """
  8. import socket
  9. import struct
  10. import traceback
  11. import time
  12. import sys
  13. import importlib
  14. # --- for linux
  15. sys.path.append('/home/sri/repositories/repositories/sri-project.demo-py/sri-dino-pyserver01')
  16. sys.path.append('/home/sri/repositories/repositories/sri-project.demo-py/3rdparty')
  17. # host, port = '127.0.0.1', 20916
  18. host, port = '127.0.0.1', 20917
  19. # --- for windows
  20. # sys.path.append(r'E:\casper\repositories\repositories\sri-project.demo-py\sri-server-bg03')
  21. # sys.path.append(r'E:\casper\repositories\repositories\sri-project.demo-py\3rdparty')
  22. # host, port = '58.34.94.178', 20916
  23. # host, port = '58.34.94.178', 20916
  24. # host, port = '58.34.94.178', 20917
  25. # --- import
  26. protobuf = importlib.import_module(f"xprotobuf.protocol_pb2")
  27. methods = importlib.import_module(f"xlib")
  28. def test():
  29. head_sequence = '<hh' # 字节序规则
  30. head_size = struct.calcsize(head_sequence)
  31. try:
  32. client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  33. # client.bind(('localhost', port))
  34. client.connect((host, port))
  35. # --- send 2004
  36. object = protobuf.Offer()
  37. object.index = 1
  38. object.peer = 1000004
  39. object.type = 'offer'
  40. object.sdp = 'aabbcc'
  41. head = 2004, object.ByteSize()
  42. head_bytestream = struct.pack(head_sequence, *head)
  43. body_bytestream = object.SerializeToString()
  44. send_bytestream = head_bytestream + body_bytestream
  45. print(f"Sent data: {send_bytestream}")
  46. client.sendall(send_bytestream)
  47. # 等待接收
  48. while True:
  49. pass
  50. except Exception as exception:
  51. print(f"#exception: {exception.__class__.__name__}")
  52. print(f"#traceback: {traceback.format_exc()}")
  53. finally:
  54. client.close()
  55. if __name__ == "__main__":
  56. test()