錯誤訊息

錯誤訊息



[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()

我在 windows 可以運作 OK

linux 在 110 行出現


  File "test.py", line 110
    finally:
          ^
SyntaxError: invalid syntax


請問這是什麼原因?


謝謝
检查所有空格和Tab
检查过了~ 不是这问题
没人知道原因吗?

谢谢
我跑了一下没报错
D:\>script1.py
Agent is serving at 23 and target to ptt.cc:23 ...
Press Ctrl + C to stop service.
Stop agent service.
我 windows 下也OK~~我是想知道为什么 linux 下会有问题@@

谢谢
用python2.4跑的吧?用2.5跑一下看,我在debian跑得没报错
debian 怎么更新再 python 2.5 ? 我怎么更新都是 2.4

谢谢
aptitude install python2.5