Guest User

Untitled

a guest
Oct 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. import socket
  2. import struct
  3. import random
  4. import datetime
  5.  
  6.  
  7. now = datetime.datetime.now()
  8.  
  9.  
  10. def id_gen(int_from=1111, int_to=9999):
  11. """Генерирует случайное число в заданом диапазоне
  12. transaction_id
  13. """
  14. m = [i for i in range(int_from, int_to)]
  15. random.shuffle(m)
  16. for id in m:
  17. yield id
  18.  
  19.  
  20. for id in id_gen():
  21. try:
  22. transaction_id = id
  23. except StopIteration:
  24. print('StopIteration error in id_gen')
  25.  
  26.  
  27. # Данные транзакции
  28. tr_header = b'zz'
  29. tr_date = hex(((now.year - 2000 << 9) | (now.month << 5) | (now.day & 31)) & 0xFFFF).encode('utf-8')
  30. tr_time = hex((now.hour << 12) | (now.minute << 6) | (now.second & 60)).encode('utf-8')
  31. # id транзакции
  32. transaction_id = hex(transaction_id).encode('utf-8')
  33.  
  34. partner_id = 35
  35. partner_id= hex(partner_id).encode('utf-8')
  36. payment = 45
  37. payment = hex(payment).encode('utf-8')
  38. tr_type = '0x00'.encode('utf-8')
  39. data = ['0x00', '0x01', '0x02', '0x03', '0x04']
  40. tr_data = random.choice(data).encode('utf-8')
  41. transaction = struct.pack('!2s6s6s4s4s4s4s4s', b'zz', tr_date, tr_time, tr_type, tr_data, transaction_id, partner_id, payment)
  42. print('transaction =', transaction)
  43.  
  44.  
  45. def client(ip, port, transaction):
  46. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
  47. sock.connect((ip, port))
  48. sock.sendall(transaction)
  49. print('Данные отправлены')
  50. sock.close()
  51.  
  52. if __name__ == "__main__":
  53.  
  54. HOST, PORT = 'localhost', 9999
  55. client(HOST, PORT, transaction)
Add Comment
Please, Sign In to add comment