Python 名称 'servers' 未定义
Python name 'servers' is not defined
我想创建一个 python 脚本来检查服务器上的服务器是否已启动。相反,如果服务器是否启动,我想在文件中捕获该结果。最后,如果服务器出现故障,那么我想收到一封电子邮件。
我使用教程 https://linuxhint.com/python-server-monitoring-script/ 开始创建具有以下结果的 python 文件。
当我执行 python 文件时,出现以下错误。我不太熟悉工作 类。所以我希望你能在这里帮助我。
$ C:/Users/../AppData/Local/Programs/Python/Python38/python.exe c:/Users/.../Documents/git/CV%20Reader%20FastAPI/CheckServer.py
Traceback (most recent call last):
File "c:/Users/../Documents/git/CV%20Reader%20FastAPI/CheckServer.py",
line 77, in <module>
servers.pickle.load(open("servers.pickle","rb"))
NameError: name 'servers' is not defined
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/Users/../Documents/git/../CheckServer.py",r.py", line 80, in <module>
Server("serveradres",80,"plain","high"),
File "c:/Users/../Documents/git/../CheckServer.py",r.py", line 12, in __init__
self.name
AttributeError: 'Server' object has no attribute 'name'
代码
from os import system
import socket
import ssl
import pickle
from datetime import datetime, time
import subprocess
import platform
class Server():
def __init__(self, name, port, connection, priority):
self.name
self.port
self.connection
self.priority
self.history = []
self.alert = False
def checkConnection(self):
msg = ""
succes = False
now = datetime.now
try:
if self.connection == "plain":
socket.create_connection((self.name,self.port),timeout=10)
msg = f"{self.name} is up. On Port {self.port} with {self.connection}"
succes = True
self.alert = False
elif self.connection == "ssl":
ssl.wrap_socket(socket.create_connection((self.name,self.port),timeout=10))
msg = f"{self.name} is up. On Port {self.port} with {self.connection}"
succes = True
self.alert = False
elif succes == False and self.alert == False:
# Send Alert
self.alert = True
# email_alert(self.name,f"{msg}\n{now}","erik.hoeven@gmail.com")
else:
if self.ping():
msg = f"{self.name} Timout on Port {self.port} with {self.connection}"
succes = True
self.alert = False
self.create_history(msg,succes,now)
except socket.timeout:
msg = f"{self.name} is Down. On Port {self.port}"
succes = False
alert = True
except:
pass
def ping(self):
try:
output = subprocess.check_output("ping - {} 1 {}".format('n' if platform.system.lower() == 'windowns' else 'c', self.name), shell = True, universal_newlines = True)
if 'unreachable' in output:
return False
else:
return True
except Exception:
return False
def create_history(self,msg,succes,now):
history_max = 100
self.history.append((msg,succes,now))
while len(self.history) > history_max:
self.history.pop(0)
if __name__ == "__main__":
try:
servers.pickle.load(open("servers.pickle","rb"))
except:
servers = [
Server("servername1",80,"plain","high"),
Server("servername2",5001,"plain","high")]
for server in servers:
server.checkConnection()
print(len(server.history))
print(server.history[-1])
pickle.dump(servers,open("serfvers.pickle","wb"))
您收到的错误是因为在 Server.__init__()
中您试图访问对象属性 name
而不是为其分配具有相同名称的参数。您正在寻找:
class Server():
def __init__(self, name, port, connection, priority):
self.name = name
...
这同样适用于所有其他参数。
此外,成员函数的缩进存在问题,因为在您的代码的当前形式中,您正在定义 local 函数而不是 class 成员函数。要解决此问题,请减少其他(非初始化)方法的缩进。
我想创建一个 python 脚本来检查服务器上的服务器是否已启动。相反,如果服务器是否启动,我想在文件中捕获该结果。最后,如果服务器出现故障,那么我想收到一封电子邮件。
我使用教程 https://linuxhint.com/python-server-monitoring-script/ 开始创建具有以下结果的 python 文件。
当我执行 python 文件时,出现以下错误。我不太熟悉工作 类。所以我希望你能在这里帮助我。
$ C:/Users/../AppData/Local/Programs/Python/Python38/python.exe c:/Users/.../Documents/git/CV%20Reader%20FastAPI/CheckServer.py
Traceback (most recent call last):
File "c:/Users/../Documents/git/CV%20Reader%20FastAPI/CheckServer.py",
line 77, in <module>
servers.pickle.load(open("servers.pickle","rb"))
NameError: name 'servers' is not defined
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/Users/../Documents/git/../CheckServer.py",r.py", line 80, in <module>
Server("serveradres",80,"plain","high"),
File "c:/Users/../Documents/git/../CheckServer.py",r.py", line 12, in __init__
self.name
AttributeError: 'Server' object has no attribute 'name'
代码
from os import system
import socket
import ssl
import pickle
from datetime import datetime, time
import subprocess
import platform
class Server():
def __init__(self, name, port, connection, priority):
self.name
self.port
self.connection
self.priority
self.history = []
self.alert = False
def checkConnection(self):
msg = ""
succes = False
now = datetime.now
try:
if self.connection == "plain":
socket.create_connection((self.name,self.port),timeout=10)
msg = f"{self.name} is up. On Port {self.port} with {self.connection}"
succes = True
self.alert = False
elif self.connection == "ssl":
ssl.wrap_socket(socket.create_connection((self.name,self.port),timeout=10))
msg = f"{self.name} is up. On Port {self.port} with {self.connection}"
succes = True
self.alert = False
elif succes == False and self.alert == False:
# Send Alert
self.alert = True
# email_alert(self.name,f"{msg}\n{now}","erik.hoeven@gmail.com")
else:
if self.ping():
msg = f"{self.name} Timout on Port {self.port} with {self.connection}"
succes = True
self.alert = False
self.create_history(msg,succes,now)
except socket.timeout:
msg = f"{self.name} is Down. On Port {self.port}"
succes = False
alert = True
except:
pass
def ping(self):
try:
output = subprocess.check_output("ping - {} 1 {}".format('n' if platform.system.lower() == 'windowns' else 'c', self.name), shell = True, universal_newlines = True)
if 'unreachable' in output:
return False
else:
return True
except Exception:
return False
def create_history(self,msg,succes,now):
history_max = 100
self.history.append((msg,succes,now))
while len(self.history) > history_max:
self.history.pop(0)
if __name__ == "__main__":
try:
servers.pickle.load(open("servers.pickle","rb"))
except:
servers = [
Server("servername1",80,"plain","high"),
Server("servername2",5001,"plain","high")]
for server in servers:
server.checkConnection()
print(len(server.history))
print(server.history[-1])
pickle.dump(servers,open("serfvers.pickle","wb"))
您收到的错误是因为在 Server.__init__()
中您试图访问对象属性 name
而不是为其分配具有相同名称的参数。您正在寻找:
class Server():
def __init__(self, name, port, connection, priority):
self.name = name
...
这同样适用于所有其他参数。
此外,成员函数的缩进存在问题,因为在您的代码的当前形式中,您正在定义 local 函数而不是 class 成员函数。要解决此问题,请减少其他(非初始化)方法的缩进。