别太复杂,方法简单就好,只要服务器没有挂,就可以执行脚本
import socket sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sk.settimeout(10) try: sk.connect(('XXXXXX,80')) print('Server port 80 OK!') except Exception: print('Server port 80 not connect!') sk.close()
pytest 有各种 fixure
可以用 curl 命令 检查返回吗,例如 : curl -I -m 15 -o /dev/null -s -w %{http_code} www.baidu.com
写个 session 级别的 fixture 来做检查
类似这样的
import urllib2 def is_runnnig(): response = None url = "http://127.0.0.1:8888" try: response = urllib2.urlopen(url, timeout=5) if str(response.getcode()).startswith("2"): return True else: return False except urllib2.URLError: return False finally: if response: response.close() ... 用while判断 while not is_runnnig(): sleep(1)