errno 107 socket.recv issue
Jordan Apgar
twistedphrame at gmail.com
Tue Feb 9 10:20:33 EST 2010
I found my car ;)
here's the server:
class commServer:
"""Class to hold a tcp server and interact with with it
allows for a wrapper around socket class to keep code clean"""
def __init__ (self, host, hostid, port, buff =1024):
self.host = host
self.hostid = hostid #id of the server
self.port = port
self.buffer = buff
self.socket = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
self.conn = None
self.addr = None
def bindServ(self):
"""Connect to the server specified by self.host, self.port"""
self.socket.bind((self.host, self.port))
def closeConn(self):
"""Disconnect from the server connected to"""
self.conn.close()
def listen(self):
self.socket.listen(1)
def accept(self):
self.conn, self.addr = self.socket.accept()
#lets you send a string msg to the server
def sendMSG(self, msg):
self.conn.send(msg)
#lets you receive data from the server
def recvMSG(self):
msg = self.socket.recv(self.buffer)
if msg == "": #if the client disconnected let's not throw
return False
else:
return msg
class Negotiator:
"""Negotiator for the server handles all communication with the
client to
verify the server and prepare the file the client wants for
download"""
def __init__(self, host, hostid, port, rsa_key):
self.server = commServer(host,hostid,port)
def Negotiate(self):
self.server.bindServ()
self.server.listen()
self.server.accept()
#Plan on being asked for server confirmation
clmsg = self.server.recvMSG() # it fails right here on the
server
calling the Server Negotiator as:
host = "127.0.0.1"
port = 8005
HOSTID = a string
key = an RSA key
servernegotiator = Negotiator(host,HostID, port, key)
if servernegotiator.Negotiate() == False:
print "something went wrong"
print "Done"
for the client it is:
class commClient:
"""Class to hold a tcp client and interact with with it
allows for a wrapper around socket class to keep code clean"""
def __init__ (self, host, hostid, port, buff =1024):
self.host = host
self.hostid = hostid #id of the server
self.port = port
self.buffer = buff
self.socket = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
def connectServ(self):
"""Connect to the server specified by self.host, self.port"""
self.socket.connect((self.host, self.port))
def disconnServ(self):
"""Disconnect from the server connected to"""
self.socket.close()
#lets you send a string msg to the server
def sendMSG(self, msg):
self.socket.send(msg)
#lets you receive data from the server
def recvMSG(self):
msg = self.socket.recv(self.buffer)
if msg == "": #if the server disconnected let's not throw
something later
return False
else:
return msg
class Negotiator:
"""The Negotiator handles all communications and message handling
necessary for verifying the server, and that the file is available
to
download"""
def __init__(self, host, hostid, port, rsa_key):
"""client should be a commClient object that has not been
connected
to the server."""
self.client = commClient(host, hostid, port)
self.clientKey = rsa_key
self.serverKey = None
self.CScipher = None #AES cipher for client -> server
self.SCcipher = None #AES cipher for server -> client
self.CShalves = None #tuple for random halves by client
self.SChalves = None #tuple for random halves by server
self.file = None
def Negotiate(self, fname):
"""Contact the server, verify the server,
negotiates for a file to be downloaded by the client. It
returns
the file name to be downloaded, and the cipher to decrypt
it."""
self.client.connectServ()
print "connected"
#tell the server you want to connect
clmsg = message(CONN, (self.client.getHost(),
self.client.getHostID())) #message acts
as a wrapper around a message type and the data for the type
self.client.sendMSG(clmsg.getSendable()) # here is were it
fails
the Negotiator is called as:
host = "127.0.0.1"
port = 8005
HOSTID is the same string as before
key is an RSA key
clientnegotiator = Negotiator(host, HostID, port, key)
filename = clientnegotiator.Negotiate("hostid")
the stack traces are:
Server side:
Traceback (most recent call last):
File "Server.py", line 17, in <module>
if servernegotiator.Negotiate() == False:
File "/home/twistedphrame/Desktop/communication/
ServerNegotiator.py", line 184, in Negotiate
clmsg = self.server.recvMSG()
File "/home/twistedphrame/Desktop/communication/
ServerNegotiator.py", line 67, in recvMSG
msg = self.socket.recv(self.buffer)
socket.error: [Errno 107] Transport endpoint is not connected
Client Side:
File "Client.py", line 17, in <module>
filename = clientnegotiator.Negotiate("hostid")
File "/home/twistedphrame/Desktop/communication/
ClientNegotiator.py", line 209, in Negotiate
srvmsg = self.client.recvMSG()
File "/home/twistedphrame/Desktop/communication/
ClientNegotiator.py", line 55, in recvMSG
msg = self.socket.recv(self.buffer)
socket.error: [Errno 104] Connection reset by peer
More information about the Python-list
mailing list