server.py 659 B

12345678910111213141516171819202122232425262728293031323334
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import sys, os
  4. from socket import socket
  5. import socketserver
  6. import http.server
  7. import threading
  8. class Server():
  9. def __init__(self, parent=None):
  10. # find free port
  11. sock = socket()
  12. sock.bind(('', 0))
  13. self._port = sock.getsockname()[1]
  14. sock.close()
  15. self.httpd = http.server.HTTPServer(('', self.port), http.server.SimpleHTTPRequestHandler)
  16. self.thread = threading.Thread(target=self.httpd.serve_forever)
  17. self.thread.daemon = True
  18. self.thread.start()
  19. print("serving at port", self._port)
  20. @property
  21. def port(self):
  22. return self._port