[英]python telnet function with __main__ global name is not defined
I'm newbie of using python. 我是使用python的新手。 I'm going to write a simple telnet program but stack in place.
我要编写一个简单的telnet程序,但要堆叠到位。
telnet.py 远程登录
def pingfunc():
global domainname
host = "1.1.1.1"
user = "user1"
password = "password2"
telnet = telnetlib.Telnet(host)
telnet.read_until('Username: ', 3)
telnet.write(user + '\r')
telnet.read_until('Password: ', 3)
telnet.write(password + '\r')
telnet.write("ping " + domainname + "\r\n")
time.sleep(3)
print telnet.read_very_eager()
if __name__ == "__main__":
global domainname
query_string = cgi.parse_qs(os.environ['QUERY_STRING'])
domainname = query_string.get('domainname', ["www.google.com"])[0]
count = query_string.get('count', [COUNT])[0]
when I call the function (pingfunc), the following error occurred: 当我调用函数(pingfunc)时,发生以下错误:
Traceback (most recent call last):
File "<string>", line 1, in <module>
telnet.write("ping " + domainname + "\r\n")
NameError: global name 'domainname' is not defined
May you help for this case? 您能为这种情况提供帮助吗?
You're using the variable domainname
in function pingfunc()
without declaring it before (or passing it to the function as an argument). 您正在函数
pingfunc()
使用变量domainname
,而无需事先声明(或将其作为参数传递给函数)。
Assuming domainname
is already defined when you're calling the function, simply change the function signature. 假设调用函数时已经定义了
domainname
,只需更改函数签名即可。
Change the line: 更改行:
def pingfunc():
with: 与:
def pingfunc(domainname):
and when you call the function, pass domainname
as an argument. 并在调用函数时将
domainname
作为参数传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.