My Goal:
- Start a local host server at example port 3000 (or 8000)
- Do a get request to check if the website is available
- Stop the localhost server from running for lets say 5 seconds
- Start the same localhost port again and check the status of it
What I have tried:
TEST CLASS
class MyTestCase(unittest.TestCase):
def test_something(self):
server = Server()
threading.Thread(target=server.create_server, daemon=True).start()
server.shutdown()
try:
requests.get('http://localhost:8090/')
except Exception as e:
raise SystemExit(e)
SERVER CLASS:
class Server():
def __init__(self):
self.httpd = None
def shutdown(self):
self.httpd.shutdown()
def create_server(self):
PORT = 8090
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as self.httpd:
print("serving at port", PORT)
print(self.httpd)
self.httpd.serve_forever()
print('never reaches this point')
My issue: Currently, my code is not able to shutdown the server. And I want to shut down the server at some point in time. I've tried server.shutdown()
& server.socket().close()
. However it still does not seem to work...
Thank you in advance!
Aucun commentaire:
Enregistrer un commentaire