F
F
fantom_ask2020-08-14 17:25:57
Python
fantom_ask, 2020-08-14 17:25:57

How to check if the server is running or not?

I want to check if the server is running before making a get request.

import requests, json
# get
url = "http://127.0.0.1:8000/"

try :
  res = requests.get(url+r'/get')
  pars= json.loads(res.content.decode('utf-8'))
  print(pars)
except ConnectionRefusedError:
  print('bad')


But how can I do this if when I turn off the server it says to me

[WinError 10061] Connection not established because the destination computer rejected the connection request


Yes, and it takes a lot of time to process the error.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SKEPTIC, 2020-08-14
@fantom_ask

You can check it only by making a test request. To reduce the waiting time, make a timeout: Number - the number of seconds, adjust as you like.
res = requests.get(url, timeout=5)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question