SocketServer error: AttributeError: instance has no __call__ method
Jordan Apgar
twistedphrame at gmail.com
Wed Feb 10 15:13:58 EST 2010
Hey guys,
I'm having some issues connecting to my Socket Server, I get this
traceback on the sever side:
----------------------------------------
Exception happened during processing of request from ('127.0.0.1',
56404)
Traceback (most recent call last):
File "/usr/lib/python2.6/SocketServer.py", line 281, in
_handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.6/SocketServer.py", line 307, in
process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.6/SocketServer.py", line 320, in
finish_request
self.RequestHandlerClass(request, client_address, self)
AttributeError: Negotiator instance has no __call__ method
----------------------------------------
here's the handler and my server:
#server side negotiator
from message import message
from sharedComs import *
from Crypto.Hash import SHA256
from Crypto.Cipher import AES
from Crypto import Random
from Crypto.PublicKey import RSA
import xmlrpclib as xmlrpc
import os
import SocketServer
class Negotiator(SocketServer.BaseRequestHandler):
CLIENT_KEY = 0
CSCIPHER = 1
SCCIPHER = 2
CSHALVES = 3
SCHALVES = 4
FILENAME = 5
def __init__(self, host, hostid, rsa_key, buf = 512):
self.host = host
self.hostid = hostid
self.serverKey = rsa_key
self.buffer = buf
self.clientKey = None
self.SCcipher = None
self.CScipher = None
self.CSHalves = None
self.SCHalves = None
self.file = None
usr_dict = {}
def setSChalves(self):
usr_dict[str(self.client_address)][SCHALVES] =
(Random.new().read(32),
Random.new().read(32))
def createCiphers(self):
"""Create the two ciphers we will be using for communication
between negotiators"""
name = str(self.client_address)
usr_dict[name][1] = AES.new(SHA256.new(str("KCS",
usr_dict[name]
[SCHALVES][0],
self.serverKey.publickey(),
usr_dict[name]
[CSHALVES][0],
usr_dict[name]
[CLIENT_KEY].publickey())).digest())
usr_dict[name][2] = AES.new(SHA256.new(str("KSC",
usr_dict[name]
[SCHALVES][1],
self.serverKey.publickey(),
usr_dict[name]
[CSHALVES][1],
usr_dict[name]
[CLIENT_KEY].publickey())).digest())
def handleConn(self, clmsg):
data = clmsg[1]
if data[0] == self.host and data[1] == self.hostid:
return True
else:
return False
def getClientHalves(self, clmsg):
"""Get the halves from the clien as well as the client key."""
print clmsg
print "clmsg[1]", clmsg[1]
data = stringToTuple(clmsg[1])
print "tuple", data
print "tuple[0]", data[0]
print "tuple[1]", data[1]
data = self.serverKey.decrypt(str(data[0]))
print
print "data:",data
print
data = stringToTuple(data)
usr_dict[str(self.client_address)][CLIENT_KEY] =
RSA.construct((data[0],data[1]))
usr_dict[str(self.client_address)][CSHALVES] = (data[2],
data[3])
def handleFileReq(self, clmsg):
"""Determine if the file requested exists:
TODO: if the client is allowed to acces"""
data = DecodeAES(self.CScipher, clmsg[1])
usr_dict[str(self.client_address)][FILENAME] = data
return os.path.exists(usr_dict[str(self.client_address)][5])
def packageFile(self):
f = open(usr_dict[str(self.client_address)][5],"rb")
data = f.read()
f.close()
crypt = EncodeAES(self.SCcipher, data)
f = open(usr_dict[str(self.client_address)]
[5]+str(self.client_address),
"wb")
f.write(crypt)
f.close
return (True,self.file+self.client.getAddr())
def handleUserDisc(self, clmsg):
os.remove(usr_dict[str(self.client_address)][FILENAME]
+str(self.client_address))
def handle(self):
#Plan on being asked for server comfirmation
msg = self.request.recv(self.buffer)
name = str(self.client_address)
clmsg = None
if not msg == "":
clmsg = xmlrpc.loads()
if not name in usr_dict:
user_dict[name] = ["","","","","",""]
print "handle Server ver"
clmsg = clmsg[0]
if clmsg[0] == CONN:
if not self.handleConn(clmsg):
print "Not right HostID"
del usr_dict[name]
else:
#We got the right message send a reply with our key
srvmsg = message(SERVER_INFO,
str(self.serverKey.publickey()))
self.request.send(xmlrpc.dumps(srvmsg.getSendable()))
elif clmsg[0] == CS_KEY:
#all this is public key encryption
#plan on getting key halves from the client
# as well as the clients public key
print "get key/halves from client"
self.getClientHalves(clmsg)
#send over our halves now
print "send halves"
self.setSCHalves()
srvmsg = message(SC_KEYDAT,
str(usr_dict[name]
[0].encrypt(usr_dict[name][4],"")))
#everyones halves were sent so generate the ciphers
self.createCiphers()
#all this is AES encryption
#### USER AUTH HERE JUST SENDING ACK FOR NOW
#no user auth yet so just acknowledge the user exists
print "send usr ack"
srvmsg = message(USER_ACK, str(EncodeAES(usr_dict[name]
[2],True)))
self.server.send(xmlrpc.dumps(servmsg.getSendable()))
#the client should be sending a file request
elif clmsg[0] == FILE_REQ:
print "expecting file req"
available = self.handleFileReq(clmsg)
if available: #file is available
srvmsg = message(FILE_ACK,
str(EncodeAES(usr_dict[name]
[2],available)))
else: #file isn't available
srvmsg = message(FILE_ACK,
str(EncodeAES(usr_dict[name]
[2],available)))
print "File %s not Available" %self.file
del usr_dict[name]
#package the file and tell the client
package = self.packageFile()
srvmsg = message(DL_READY, EncodeAES(usr_dict[name][2],
package))
self.server.send(xmlrpc(srvmsg.getSendable()))
if not package[0]: # the file wasn't pakaged so close the
connection
#the client tells us they got the file so just remove the file
that
#was made
print "expecting USER_DISC"
else:
if not usr_dict[name][5] == "":
handleUserDisc(clmsg)
del usr_dict[name]
class ServerNegotiator:
def __init__(self, host, port, hostid, rsa_key, buf = 512):
negotiator = Negotiator(host, hostid, rsa_key,buf)
self.server = SocketServer.TCPServer((host, port), negotiator)
def start(self):
self.server.serve_forever()
Do I need to write the __call__() method as well?
More information about the Python-list
mailing list