python oserror errorno 39_python的bug? OSError: [Errno 9] Bad file number

该博客内容涉及到一个Python脚本使用pexpect库进行telnet连接并执行命令时遇到的问题。在尝试登录远程主机并执行一系列操作后,程序抛出`OSError: [Errno 9] Bad file number`错误。作者提到此问题在不同Python版本和Solaris系统上重现,且排除了网络问题。代码示例展示了如何使用pexpect进行telnet会话,并包含了测试用例的设置和执行部分。
摘要由CSDN通过智能技术生成

前面发贴问了怎么定义一个全局变量,后来发现不是变量作用范围的问题。

运行log:

root@Pandora # ./TestCase.py

Traceback (most recent call last):

File "./TestCase.py", line 44, in

TC.setup()

File "./TestCase.py", line 31, in setup

GWchild.sendline("echo i am setup > setup")

File "/usr/sfw/lib/python2.5/site-packages/pexpect.py", line 729, in sendline

n = self.send(str)

File "/usr/sfw/lib/python2.5/site-packages/pexpect.py", line 722, in send

c = os.write(self.child_fd, str)

OSError: [Errno 9] Bad file number

我在python 2.5.1, 2.4.2,2.3.3都运行过,一样的问题.两台机器都是内部网,网络质量上肯定没问题。

在Solaris 5.8,5.9, 5.10都试过了,也一样的错误

下面是code:

#!/usr/bin/env python

import unittest,pexpect,time

def login(host,account,accountPasswd,RootPasswd):

child= pexpect.spawn ('telnet '+host)

child.expect ('login:')

child.sendline (account)

child.expect ('Password:')

child.sendline (accountPasswd)

child.expect('$')

child.sendline('su -')

child.expect('Password:')

child.sendline(RootPasswd)

child.sendline('bash')

child.expect('#')

fout=open("log","a")

child.logfile=fout

return child

GWchild=login("192.168.22.236","nocadm","S1n0cadM","Bjsroot")

GWchild.sendline("cd /export/home/nocadm/")

GWchild.sendline("echo i am outer > out")

#MidHandle.restartASG(GWchild,"cm.TCSBM2Korean")

#class TC2_1_2(unittest.TestCase):

class TC2_1_2:

def setup(self):

GWchild.sendline("echo i am setup > setup")

def testTC2_1_2(self):

GWchild.sendline("echo i am testTC2_1_2 > case")

def teardown(self):

pass

time.sleep(5)

GWchild.close()

if __name__=="__main__":

TC=TC2_1_2()

TC.setup()

TC.testTC2_1_2()

谢谢

[本帖最后由 powerccna 于 2007-11-12 23:30 编辑]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值