tcp - send a variable to a TCPHandler in python -


i'm confused how send variable tcphandler using socketserver.tcpserver in python..

host, port = hosts[0], args.port server = socketserver.tcpserver((host, port), metcphandler) server.serve_forever() 

which calls:

class metcphandler(socketserver.baserequesthandler):                def handle(self):         self.data = self.request.recv(1024).strip()         print "{} wrote:".format(self.client_address[0])         r = mexpresshandler(self.data, false) 

but want pass debug boolean mexpresshandler.. so

host, port = hosts[0], args.port server = socketserver.tcpserver((host, port), metcphandler(debug)) server.serve_forever() 

fails. whats correct way of doing this? have recreate whole tcphandler over-ridding __init__?

trust instincts, correct way is indeed subclass tcpserver , override __init__ method, python makes easy!

import socketserver  class debugtcpserver(socketserver.tcpserver):     def __init__(self, server_address, requesthandlerclass, bind_and_activate=true, debug=true):         self.debug = debug         socketserver.tcpserver.__init__(self, server_address, requesthandlerclass, bind_and_activate=true)  class debugmetcphandler(socketserver.baserequesthandler):     def handle(self):         # self.server instance of debugtcpserver         debug = self.server.debug         self.data = self.request.recv(1024).strip()         if debug:             print "{} wrote:".format(self.client_address[0])         r = mexpresshandler(self.data, false)   server = debugtcpserver((host, port), debugmetcphandler, debug=true) 

or since specified debug=true default:

server = debugtcpserver((host, port), debugmetcphandler) 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -