未定义全局名称为\uuu main\uuu的python telnet函数

2024-09-30 22:15:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我是使用python的新手。我将要编写一个简单的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]

当我调用函数(pingfunc)时,出现以下错误:

^{pr2}$

你能帮上忙吗?在


Tags: hostreadgetstringcountpasswordqueryglobal
1条回答
网友
1楼 · 发布于 2024-09-30 22:15:30

在函数pingfunc()中使用变量domainname,而没有在之前声明它(或将其作为参数传递给函数)。在

假设调用函数时已经定义了domainname,只需更改函数签名即可。在

换行:

def pingfunc():

有:

^{pr2}$

当您调用函数时,传递domainname作为参数。在

相关问题 更多 >