gpsdfake 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # This file is Copyright 2010 by the GPSD project
  2. # SPDX-License-Identifier: BSD-2-clause
  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:
  43. # vim: set expandtab shiftwidth=4