client.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # -*- coding: utf-8 -*-
  2. import json
  3. import logging
  4. import os, sys, time
  5. import socket
  6. import datetime
  7. class logClient:
  8. def __init__(self):
  9. self.port = 5566
  10. self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  11. self.constate = False
  12. def __del__(self):
  13. self.disconnect()
  14. # 连接服务器;
  15. def connect(self):
  16. try:
  17. self.sock.connect(('127.0.0.1', self.port))
  18. self.constate = True
  19. except Exception, e:
  20. print "LoggingUtil:client=> socket connect error:", e, self.port
  21. self.constate = False
  22. return self.constate
  23. # 发送消息;
  24. def sendmsg(self, msg):
  25. if self.constate is False:
  26. if self.connect() is False:
  27. return
  28. msg_json = ""
  29. msg_dict = {'ReportType': 'printLog', 'prinMsg': msg}
  30. try:
  31. msg_json = json.dumps(msg_dict)
  32. except Exception:
  33. print "LoggingUtil:client=> to json error:", msg
  34. return
  35. try:
  36. self.sock.settimeout(1)
  37. self.sock.sendall(msg_json)
  38. self.sock.settimeout(None)
  39. except Exception, e:
  40. print "LoggingUtil:client=>无Runner接收结果。Error:", e, 'runner_sender_port:', self.port
  41. self.disconnect()
  42. self.connect()
  43. # 断开连接;
  44. def disconnect(self):
  45. try:
  46. self.sock.shutdown(2)
  47. self.sock.close()
  48. except Exception, e:
  49. print "LoggingUtil:client=> socket disconnect error:", e
  50. # 单例模块;
  51. logClient_singleton = logClient()
  52. def current_time():
  53. return time.strftime('%Y-%m-%d %H:%M:%S ', time.localtime(time.time()))
  54. def print_log(tag='', logmsg=''):
  55. if logmsg == '':
  56. msg = current_time() + 'printLog: %s' % str(tag)
  57. tcp_msg = str(tag)
  58. else:
  59. msg = current_time() + 'printLog: %s:%s' % (str(tag), str(logmsg))
  60. tcp_msg = str(tag) + str(logmsg)
  61. print msg
  62. logClient_singleton.sendmsg(tcp_msg)
  63. if __name__ == '__main__':
  64. # print_log("printLog", "测试中……")
  65. # print_log(unicode("this step is: Index : " + "1" + " Name: " + str("1")+ " Desc: " + str("1") + " result: " + "Detail"))
  66. # print_log("this step is: Index : " + "我" + " Name: " + str("1")+ " Desc: " + str("1") + " result: " + "Detail")
  67. # test_basic_general("1.jpg")
  68. # test_basic_acurate("1.jpg")
  69. # test_getStrWithImgProcess()
  70. # print_log("printLog", "Test_Fail:Timing%s,%s截图,红场的比例与标准比例差异过大!
")
  71. while True:
  72. time.sleep(5)
  73. print_log("printLog", "测试中……")
  74. # print_log("printLog", "Test_Fail:Timing%s,%s截图,红场的比例与标准比例差异过大!
")
  75. # time.sleep(2)
  76. # # print_log("printLog", u"测试中……")