socket_client.py 1023 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # -*- coding:utf8 -*-
  2. #!/usr/bin/env python
  3. import threading
  4. import hashlib
  5. import socket
  6. import base64
  7. import time
  8. import json
  9. #创建一个客户端的socket对象
  10. client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  11. #设置服务端的ip地址
  12. host = "127.0.0.1"
  13. # host = "192.168.1.53"
  14. #设置端口
  15. port = 9999
  16. #连接服务端
  17. client.connect((host, port))
  18. data = {'client_id':'1001','at':25,'ah':65}
  19. #while循环是为了保证能持续进行对话
  20. # while True:
  21. # pass
  22. # 输入发送的消息
  23. # # sendmsg = input("请输入:")
  24. # sendmsg = json.dumps(data)
  25. # #如果客户端输入的是q,则停止对话并且退出程序
  26. # # if sendmsg=='q':
  27. # # break
  28. # sendmsg = sendmsg
  29. # #发送数据,以二进制的形式发送数据,所以需要进行编码
  30. # client.send(sendmsg.encode("utf-8"))
  31. # msg = client.recv(1024)
  32. #接收服务端返回的数据,需要解码
  33. # print(msg.decode("utf-8"))
  34. # time.sleep(5)
  35. #关闭客户端
  36. # client.close()