幫忙看一下代碼錯誤在哪
[Copy to clipboard] [ - ]
CODE:
# -*- coding: utf-8 -*-
import socket
import asyncore
# Agent service服务的port
servicePort = 23
# 目标主机的位置与Port
serviceHostAddress = 'ptt.cc'
serviceHostPort = 23
class AgentClient(asyncore.dispatcher_with_send):
"""服务host和client端的sokcet物件"""
def __init__(self, otherSocket=None):
asyncore.dispatcher_with_send.__init__(self, otherSocket)
# 是否为client
self.client = False
# 是否已准备好传送资料给对方
self.ready = False
# 另一端
self.other = None
if not otherSocket:
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
else:
self.client = True
def handle_error(self):
"""处理连线发生错误"""
print "Error"
self.handle_close()
def handle_read(self):
"""读取资料并传送到另一端"""
if self.other and self.other.ready:
data = self.recv(4096)
# 检查是否有资料,没资料可能表示已断线,传送会造成错误
if data:
self.other.send(data)
def handle_connect(self):
"""连线成功,设定成ready状态"""
self.ready = True
self.other.ready = True
def handle_close(self):
""""连线被关闭,同时关闭另一端连线"""
if self.client:
print "Close connection from : " + self.socket.getpeername()[0]
self.ready = False
self.close()
if self.other and self.other.ready:
self.other.handle_close()
class AgentServer(asyncore.dispatcher):
"""中继服务Server的socket物件,负责接听连线"""
def __init__(self, port):
asyncore.dispatcher.__init__(self)
self.port = port
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.bind(('', self.port))
self.listen(5)
def handle_accept(self):
"""接收连线"""
clientSocket, address = self.accept()
print 'New client from : ' + address[0]
agentClient = AgentClient(clientSocket)
agentHost = AgentClient()
# 连线到另一端,可能会连线失败
try:
agentHost.connect((serviceHostAddress, serviceHostPort))
except socket.error, (socketError, errorMessage):
print 'Failed to connect %s : %s' % (serviceHostAddress, errorMessage)
agentClient.handle_close()
return
agentHost.other = agentClient
agentClient.other = agentHost
def cleanSocketMapUp():
"""清除所有正在运作的Socket"""
# 使用keys()传回与map无关的keys来关闭socket以避免发生 :
# RuntimeError: dictionary changed size during iteration
for fileno in asyncore.socket_map.keys():
asyncore.socket_map[fileno].close()
agentServer = AgentServer(servicePort)
print "Agent is serving at %d and target to %s:%d ..." % (servicePort, serviceHostAddress, serviceHostPort)
print "Press Ctrl + C to stop service."
try:
asyncore.loop(1)
except KeyboardInterrupt:
print "Stop agent service."
finally:
agentServer.close()
cleanSocketMapUp()
shihyu-pc:~# python ttt.py
File "ttt.py", line 110
finally:
^
SyntaxError: invalid syntax
请问我 line 110 finally: 错在哪边?
谢谢