Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3-导入和重写方法_Python_Sockets_Import_Overriding - Fatal编程技术网

Python3-导入和重写方法

Python3-导入和重写方法,python,sockets,import,overriding,Python,Sockets,Import,Overriding,在server.py中,我有两个类,classserver和ClientThread方法processCmd。我想在将此代码导入到新代理类时重写此方法。 怎么做 这是server.py: class ClientThread( threading.Thread, socket.socket ): #(some code) def processCmd( self, cmd ): if 'quit' == cmd: self.writelin

server.py
中,我有两个类,class
server
ClientThread
方法
processCmd
。我想在将此代码导入到新代理类时重写此方法。 怎么做

这是server.py:

class ClientThread( threading.Thread, socket.socket ):
    #(some code)
    def processCmd( self, cmd ):
        if 'quit' == cmd:
            self.writeline(str('Ok, bye'))
            QUIT = True
            done = True
        elif 'bye' == str(cmd):
            self.writeline(str('Ok, bye'))
            done = True
        else:
            print(cmd)
#
class Server:
    #(some code)
    def run( self ):
       self.sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
       self.sock.bind( ( '127.0.0.1', 5050 ) )
       new_thread = ClientThread( client )
       print('Incoming Connection. Started thread ', end=' ')
       self.thread_list.append( new_thread )
       new_thread.start()
在另一个文件中是主代码:

import server
#
class Proxy( server.Server):
    def processCmd( self, cmd ):
        if 'quit' == cmd:
            self.writeline(str('Another quit'))
            QUIT = True
            done = True
        elif 'bye' == str(cmd):
            self.writeline(str('Another bye'))
            done = True
        else:
            print(cmd)
    pass

您不能覆盖
服务器
类中的
processCmd
方法。您还必须导入
ClientThread
,并将其子类化

import server

class Proxy( server.Server):
    def run( self ):
       self.sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
       self.sock.bind( ( '127.0.0.1', 5050 ) )
       new_thread = CustomClientThread( client )
       print('Incoming Connection. Started thread ', end=' ')
       self.thread_list.append( new_thread )
       new_thread.start()
    pass

class CustomClientThread(server.ClientThread)
    def processCmd( self, cmd ):
        #override here

如果你不想覆盖整个
run
方法,你应该在基类中修改它,这样clientThread的类型就可以设置在方法的一侧

class Server:
    thread = ClientThread

    #(some code)
    def run( self ):
       self.sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
       self.sock.bind( ( '127.0.0.1', 5050 ) )
       new_thread = thread( client )
       print('Incoming Connection. Started thread ', end=' ')
       self.thread_list.append( new_thread )
       new_thread.start()
然后子类看起来就像这样,
run
方法保持不变

class Proxy( server.Server):
    thread = CustomClientThread

如果您想从
ClientThread
重写方法,则可以按如下方式重写

class OverrideClientThread(server.ClientThread):
    def processCmd(self, cmd):
        # call super
        server.ClientThread.processCmd(self, cmd)
        #do something.

我在类
Server
中看不到
processCmd
方法。感谢您的回复,我已将:new\u thread=ClientThread(client)更改为thread=ClientThread和new\u thread=thread(client),但我在启动旧ClientThread进程时遇到问题