SendMessage.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import socket
  2. nextcloud = '/var/snap/nextcloud/common/nextcloud/data/zymwin/files'
  3. def send_message(msg_type,id,msg):
  4. client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  5. ip = '127.0.0.1'
  6. client.connect((ip, 5700))
  7. #将msg中特殊字符进行url编码
  8. msg = msg.replace(" ", "%20")
  9. msg = msg.replace("\n", "%0a")
  10. if msg_type == 'group':
  11. payload = "GET /send_group_msg?group_id=" \
  12. + str(id) + "&message=" + msg + " HTTP/1.1\r\nHost:" \
  13. + ip + ":5700\r\nConnection: close\r\n\r\n"
  14. elif msg_type == 'private':
  15. payload = "GET /send_private_msg?user_id=" \
  16. + str(id) + "&message=" + msg + " HTTP/1.1\r\nHost:" \
  17. + ip + ":5700\r\nConnection: close\r\n\r\n"
  18. print("发送" + payload)
  19. client.send(payload.encode("utf-8"))
  20. client.close()
  21. return 0
  22. def upload_file(msg_type,id,file,name):
  23. client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  24. ip = '127.0.0.1'
  25. client.connect((ip, 5700))
  26. #将msg中特殊字符进行url编码
  27. file = file.replace(" ", "%20")
  28. file = file.replace("\n", "%0a")
  29. if msg_type == 'group':
  30. payload = "GET /upload_group_file?group_id=" \
  31. + str(id) + "&file=" + file + "&name=" + name + " HTTP/1.1\r\nHost:" \
  32. + ip + ":5700\r\nConnection: close\r\n\r\n"
  33. elif msg_type == 'private':
  34. payload = "GET /upload_private_file?user_id=" \
  35. + str(id) + "&file=" + file + " HTTP/1.1\r\nHost:" \
  36. + ip + ":5700\r\nConnection: close\r\n\r\n"
  37. print("发送" + payload)
  38. client.send(payload.encode("utf-8"))
  39. client.close()
  40. return 0