Advertisement
Guest User

Untitled

a guest
May 8th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import socket
  4. import os
  5. import threading
  6. import mysql.connector
  7. import time
  8. import datetime
  9. Dconn = mysql.connector.connect(user='root', password='peiman64',host='localhost',database='prisoner',charset='utf8',autocommit=True)
  10. cursor = Dconn.cursor(buffered=True)
  11. cursor2 = Dconn.cursor(buffered=True)
  12. print('databased stablished.')
  13. def sendData(server,port,sig) :
  14. # now = datetime.datetime.now()
  15. print(format(server)+':'+format(port)+' - '+format(sig[0]))
  16. cursor2.execute("select * from raw join devices on devices.dvid=raw.device where raw.rdid='"+format(sig[1])+"' limit 1;")
  17. sendSig = cursor2.fetchone()
  18. print(sendSig)
  19. clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  20. result = clientsocket.connect((server,port))
  21. timezonedata = datetime.datetime.fromtimestamp(int(sendSig[12]+14400))
  22. sistr='$MGV002,'+sendSig[18]+',12345,S,'+timezonedata.strftime("%d%m%y")+','+timezonedata.strftime("%H%M%S")+',A,'+format(sendSig[2])+',N,0'+format(sendSig[3])+',E,0,'+format(sendSig[5])+',00,1.95,'+format(sendSig[4])+',0,,,432,$
  23. print(sistr)
  24. clientsocket.send(sistr)
  25. while True:
  26. data = clientsocket.recv(1024)
  27. print ("Received response:" + str(data))
  28. break
  29. # if data.decode('utf8')=='#AL#1###rn':
  30. sqlobject = "UPDATE `row_sent` SET `result`='succes',`send_date`=NOW() WHERE `rsid`='"+format(sig[0])+"' limit 1;"
  31. try:
  32. for result in cursor.execute(sqlobject,multi = True):
  33. pass
  34. except mysql.connector.Error as err:
  35. print("Something went wrong: {}".format(err))
  36. clientsocket.close()
  37. while True:
  38. cursor.execute("select * from row_sent where result='pending' limit 1;")
  39. if cursor.rowcount>0 :
  40. waitedSig = cursor.fetchone()
  41. cursor.execute("select * from servers where `status`='active';")
  42. if cursor.rowcount>0 :
  43. fields=cursor.fetchall()
  44. for field in fields:
  45. t = threading.Thread(target=sendData , args=((field[3]),(field[4]),(waitedSig),))
  46. t.start()
  47. time.sleep(0.5)
  48. else:
  49. time.sleep(1)
  50. print('he...')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement