Python论坛  - 讨论区

标题:Re: [python-chinese] timeout的问题

2005年05月01日 星期日 22:14

Qiangning Hong hongqn at gmail.com
Sun May 1 22:14:11 HKT 2005

出错信息指明了Timeout异常是在执行connect的时候出现的,因此你要把connect调用也包括到try块中来,像这样:

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
s.connect((host,81))
s.send(request)
buf = s.recv(1000)
except timeoutsocket.Timeout:
print 'Timeout!'
else:
print buf

On 5/1/05, . newbie <learnpython at gmail.com> wrote:
> 
> 我的代码大体是这样的:
> import sys
> import socket
> import time
> import timeoutsocket
> timeoutsocket.setDefaultSocketTimeout(2)
> 
> def connect(request):
> host = "192.168.0.1 <http://192.168.0.1>"
> s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
> s.connect((host,81))
> s.send(request)
> try:
> s.recv(1000)
> except timeoutsocket.Timeout:
> print 'Timeout!'
> else:
> print s.recv(1000)
> 
> a1 = ['a', 'b', 'c']
> a2 = ['a', 'b', 'c']
> for i in a1:
> for j in a2:
> request = i + j
> connect(request)
> 
> 运行之后, File "D:\myprogram\timeoutsocket.py", line 238, in conne
> ct
> raise Timeout("Attempted connect to %s timed out." % str(addr) )
> timeoutsocket.Timeout: Attempted connect to ('192.168.0.1<http://192.168.0.1>', 
> 81) timed out.
> 
> 程序还是退出了,我想即使timeout出现,循环能继续执行,怎么做?
> 我比较愚钝,大家再指点一下
> 多谢
> 在05-5-1,Qiangning Hong<hongqn at gmail.com> 写道:
> > 
> google了一下,你说的是http://www.steffensiebert.de/soft/python/timeoutsocket.py吗?
> >
> > timeoutsocket.py的docstring里有这样的描述:
> >
> > The timeout applies to the socket functions that normally block on
> > execution: read, write, connect, and accept. If any of these
> > operations exceeds the specified timeout, the exception Timeout
> > will be raised.
> >
> > 可以看出,超时时会抛出异常。你只需要用try...except...机制捕获这个异常进行处理就好了。
> >
> > 例如:
> >
> > while keep_going:
> > try:
> > buf = sck.recv(10)
> > except timeoutsocket.Timeout:
> > print 'Timeout!'
> > else:
> > do_something(buf)
> >
> >
> >
> > On 5/1/05, . newbie <learnpython at gmail.com> wrote:
> > >
> > > 我在程序里用了Timothy O'Malley写的timeoutsocket.py的timeout功能,
> > > 在程序开头这样写:
> > > import timeoutsocket
> > > timeoutsocket.setDefaultSocketTimeout(2)
> > >
> > > 超时值设成了两秒。
> > > 超时后,程序就退出了
> > >
> > > 问题是,假如我是一个循环,超时后我想让循环继续,而不是退出,该怎么做?
> > >
> > > 这个问题类似于C里面exit和return一样,我想要的只是它return一个值给我,而不是exit(1),
> > >
> > > 该怎么解决?
> > >
> > > 多谢
> > >
> > > _______________________________________________
> > > python-chinese list
> > > python-chinese at lists.python.cn
> > > http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> > >
> >
> >
> >
> > --
> > Qiangning Hong
> > Get Firefox!
> > <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1
> > >
> >
> 



-- 
Qiangning Hong
Get Firefox! <
http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050501/1e7d5a8b/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年05月01日 星期日 22:19

Xia Gao xiaogao at mails.tsinghua.edu.cn
Sun May 1 22:19:57 HKT 2005

问题已经解决了,像下面那样写就可以了:
filePath = unicode(QFileDialog.getOpenFileName(None, "All Files (*)", self))
text = codecs.open(filePath, "r", "mbcs").read().encode("utf-8")
用unicode()函数将getOpenFileName对话框返回的文件名转换为unicode,就可以识别和显示了。
PyXEdit是我写来练手的一个代码编辑器,是基于PyQScintilla的;像eric也是基于PyQScintilla的。

======== 2005-05-01 22:08:20 您在来信中写道: ========

我没有用过PyQT,不过从出错信息来看,我觉得是filePath.ascii()的问题。
这个PyXEdit是个什么东东?



On 5/1/05, Xia Gao <xiaogao at mails.tsinghua.edu.cn> wrote:
大家好!

        我是Python的初学者,今天用PyQT遇到一个问题:当用"打开"对话框打开文件时,如果不是中文文件名,就可以打开;否则会出现以下异常:
Traceback (most recent call last):
  File "F:\Python\PyXEdit\QXEMainWindow.py", line 132, in fileOpen
    self.loadFile (filePath)
  File "F:\Python\PyXEdit\QXEMainWindow.py", line 158, in loadFile
    text = codecs.open(filePath.ascii().encode("mbcs"), "r", "mbcs").read().enco
de("utf-8") 
  File "C:\Python24\lib\codecs.py", line 607, in open
    file = __builtin__.open(filename, mode, buffering)
IOError: [Errno 2] No such file or directory: 'F:/Python/PyXEdit/Test?????.txt'
问题应该是中文文件名不能处理。请大家帮忙想想办法,谢谢! 

致
礼!


Xia Gao
xiaogao at mails.tsinghua.edu.cn
2005-05-01

_______________________________________________
python-chinese list
python-chinese at lists.python.cn
http://python.cn/mailman/listinfo/python-chinese






-- 
Qiangning Hong
Get Firefox! < http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>


= = = = = = = = = = = = = = = = = = = = = = 
        致
礼!

              Xia Gao
              xiaogao at mails.tsinghua.edu.cn
               2005-05-01

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号