Devuan fork of gpsd
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.
 
 
 
 
 
 

51 lines
1.7 KiB

  1. # This file is Copyright (c) 2010 by the GPSD project
  2. # BSD terms apply: see the file COPYING in the distribution root for details.
  3. """
  4. gpsdfake - a fake gpsd server that spews specified data at gpsd clients.
  5. """
  6. import sys, SocketServer
  7. class FakeHandler(SocketServer.BaseRequestHandler):
  8. "Instantiated once per connection to the server."
  9. def handle(self):
  10. global lines
  11. # self.request is the TCP socket connected to the client
  12. # Read the client's ?WATCH request.
  13. self.data = self.request.recv(1024).strip()
  14. # We'd like to send a fake banner to the client on startup,
  15. # but there's no (documented) method for that. We settle
  16. # for shipping on first request.
  17. self.request.send('{"class":"VERSION",'
  18. '"version":"gpsdfake","rev":"gpsdfake",'
  19. '"proto_major":3,"proto_minor":1}\r\n')
  20. # Perpetually resend the data we have specified
  21. while True:
  22. for line in lines:
  23. self.request.send(line)
  24. if __name__ == "__main__":
  25. (HOST, PORT) = "localhost", 2947
  26. try:
  27. if len(sys.argv) <= 1:
  28. sys.stderr.write("gpsdfake: requires a file argument.\n")
  29. sys.exit(1)
  30. lines = open(sys.argv[1]).readlines()
  31. # Create the server, binding to localhost on port 2947
  32. server = SocketServer.TCPServer((HOST, PORT), FakeHandler)
  33. # Activate the server; this will keep running until you
  34. # interrupt the program with Ctrl-C
  35. server.serve_forever()
  36. except KeyboardInterrupt:
  37. pass
  38. sys.exit(0)
  39. # The following sets edit modes for GNU EMACS
  40. # Local Variables:
  41. # mode:python
  42. # End: