python try模块疑问

xiaolomg 2015-08-08 07:22:24
在网上看到一段代码 :
import socket
s = socket.socket()
s.setblocking(0)

try:
s.connect(('www.baidu.com', 80))
except socket.error as e:
print(str(e))
i = 0
while True:
try:
print("We are connected to %s:%d" % s.getpeername())
break
except:
print("Let's do some math while waiting: %d" % i)
i += 1
else:
print("We are connected to %s:%d" % s.getpeername())



代码会一直执行到socket链接上
不知道循环时怎么建立起来的
...全文
130 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsjfdjoijvtghu 2015-08-10
  • 打赏
  • 举报
回复
假如s.getpeername())也出exception,那么就陷入死循环了
似梦飞花 2015-08-08
  • 打赏
  • 举报
回复
没有阿 当出错时 print("We are connected to %s:%d" % s.getpeername()) break 执行这句后跳出不会再执行其他的了 循环只是验证 print("We are connected to %s:%d" % s.getpeername()) 这句输出有没有报错

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧