You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fromUDP.py 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. # -*- mode: Python -*-
  4. '''
  5. fromUDP
  6. Udp server to cli
  7. v0.1b
  8. '''
  9. from __future__ import print_function
  10. import traceback, time
  11. import argparse
  12. import socket
  13. import _thread
  14. import sys
  15. name="generator::fromUDP"
  16. def debug(*args, **kwargs):
  17. if( verbose == False ):
  18. return
  19. print(*args, file=sys.stderr, **kwargs)
  20. argsparser = argparse.ArgumentParser(description="fromUDP v0.1b help mode")
  21. argsparser.add_argument("-v","--verbose",action="store_true",help="Verbose output")
  22. argsparser.add_argument("-i","--ip",help="IP to bind to (0.0.0.0 by default)",default="0.0.0.0",type=str)
  23. argsparser.add_argument("-p","--port",help="UDP port to bind to (9000 by default)",default=9000,type=str)
  24. args = argsparser.parse_args()
  25. verbose = args.verbose
  26. ip = args.ip
  27. port = int(args.port)
  28. verbose = args.verbose
  29. def udp_thread():
  30. while True:
  31. payload, client_address = sock.recvfrom(1024)
  32. udpath = payload.decode('utf_8')
  33. debug(udpath[0:])
  34. print(udpath[0:], flush=True);
  35. '''
  36. # Reply to client
  37. bytesToSend = str.encode("ACK :"+str(payload))
  38. serverAddressPort = (client_address, port)
  39. bufferSize = 1024
  40. #sock.sendto(bytesToSend, serverAddressPort)
  41. sock.sendto(bytesToSend, client_address)
  42. '''
  43. def StartUDP(serverIP, UDPORT):
  44. global sock
  45. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  46. server = ( serverIP,UDPORT)
  47. sock.bind(server)
  48. _thread.start_new_thread(udp_thread, ())
  49. StartUDP(ip, port)
  50. # Do something else
  51. try:
  52. while True:
  53. time.sleep(0.005)
  54. except Exception:
  55. traceback.print_exc()