p.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/python
  2. from sys import stdout, stdin, exit
  3. import sys
  4. import traceback
  5. import os
  6. lockFile = "/var/lock/socketLock"
  7. c = ""
  8. exists = os.path.isfile(lockFile)
  9. if exists:
  10. stdout.write("socket is already connected.")
  11. stdout.flush()
  12. exit()
  13. else:
  14. with open(lockFile, 'a'):
  15. os.utime(lockFile, None)
  16. # Store configuration file values
  17. while True:
  18. s = stdin.readline().replace('\n','') # raw_input()
  19. if s == 'exit':
  20. os.remove(lockFile)
  21. exit()
  22. if s == 'go':
  23. description='source string'
  24. try:
  25. exec(c)
  26. stdout.flush()
  27. c = ""
  28. except SyntaxError as err:
  29. error_class = err.__class__.__name__
  30. detail = err.args[0]
  31. line_number = err.lineno
  32. stdout.write(("%s at line %d of %s: %s\n\n" % (error_class, line_number, description, detail)))
  33. stdout.flush()
  34. c = ""
  35. except Exception as err:
  36. error_class = err.__class__.__name__
  37. detail = err.args[0]
  38. cl, exc, tb = sys.exc_info()
  39. line_number = traceback.extract_tb(tb)[-1][1]
  40. stdout.write(("%s at line %d of %s: %s\n\n" % (error_class, line_number, description, detail)))
  41. stdout.flush()
  42. c = ""
  43. else:
  44. s.replace('print','stdout.write')
  45. c += s + '\n'