errno 107 socket.recv issue

2010-02-09 Thread Jordan Apgar
I have a simple tcp server and client where the server sits and waits
for a message and then processes it, my client sends its first message
to the server.  On the server I receive:

socket.error: [Errno 107] Transport endpoint is not connected
when calling
msg = self.socket.recv(self.buffer)

My client receives the error:
socket.error: [Errno 104] Connection reset by peer
when calling
msg = self.socket.recv(self.buffer)

I was working on the server and client over the weekend and sending
and receiving worked fine, I wanted to debug a few things and I get
this when I try to run it (no changes made from what I had on the
weekend)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: errno 107 socket.recv issue

2010-02-09 Thread Jordan Apgar
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 
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] T

Re: errno 107 socket.recv issue

2010-02-09 Thread Jordan Apgar

> http://docs.python.org/library/socketserver.html
>
> JM

each time a handler is spawned is it client specific? in other words
when two clients send something to the server do handlers spawn for
each of them or does everything just go into a single handler?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: errno 107 socket.recv issue

2010-02-09 Thread Jordan Apgar
thanks JM,

at this point i switched over to this scheme and now I'm getting an
error durring instantiation of the server:
Server.py:
from Crypto.PublicKey import RSA
from ServerNegotiator import ServerNegotiator
from sharedComs import *

f = open("hostid")
tup = stringToTuple(f.readline()[0:-1])
HostID = f.readline()[0:-1]
f.close()

key = RSA.construct((long(tup[0]),long(tup[1]), long(tup[2]),
long(tup[3]),
 long(tup[4]),long(tup[5])))
host = "localhost"
port = 8005

servernegotiator = ServerNegotiator(host,HostID, port, key)
servernegotiator.start()


ServerNegotiatior.py lines 185 - end
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()





Traceback (most recent call last):
  File "Server.py", line 16, in 
servernegotiator = ServerNegotiator(host,HostID, port, key)
  File "/home/twistedphrame/Desktop/communication/
ServerNegotiator.py", line 188, in __init__
self.server = SocketServer.TCPServer((host, port), negotiator)
  File "/usr/lib/python2.6/SocketServer.py", line 400, in __init__
self.server_bind()
  File "/usr/lib/python2.6/SocketServer.py", line 411, in server_bind
self.socket.bind(self.server_address)
  File "", line 1, in bind
TypeError: an integer is required

-- 
http://mail.python.org/mailman/listinfo/python-list


Pycrypto RSA Issue

2010-02-09 Thread Jordan Apgar
I am trying to encrypt public data along with another tuple and then
decrypt it after sending. RSA is needed for negotiation of keys for
AES. But I just get garbage after I decrypt it. This is what I'm
attempting to do:

from Crypto.PublicKey import RSA
from Crypto import Random

gkey = RSA.generate(384, Random.new().read)
string = str((gkey.publickey().__getstate__(),
  (333,444)))
print string
print
data = gkey.encrypt(string,"")
print "data:",data
print
print "decrypt"
print "decrypt,", gkey.decrypt(data)
print "done"

All I get are junk values when printing the decrypted value.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: errno 107 socket.recv issue

2010-02-09 Thread Jordan Apgar
On Feb 9, 1:51 pm, Jean-Michel Pichavant 
wrote:
> Jordan Apgar wrote:
> > thanks JM,
>
> > at this point i switched over to this scheme and now I'm getting an
> > error durring instantiation of the server:
> > Server.py:
> > from Crypto.PublicKey import RSA
> > from ServerNegotiator import ServerNegotiator
> > from sharedComs import *
>
> > f = open("hostid")
> > tup = stringToTuple(f.readline()[0:-1])
> > HostID = f.readline()[0:-1]
> > f.close()
>
> > key = RSA.construct((long(tup[0]),long(tup[1]), long(tup[2]),
> > long(tup[3]),
> >                      long(tup[4]),long(tup[5])))
> > host = "localhost"
> > port = 8005
>
> > servernegotiator = ServerNegotiator(host,HostID, port, key)
> > servernegotiator.start()
>
> > ServerNegotiatior.py lines 185 - end
> > 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()
>
> > Traceback (most recent call last):
> >   File "Server.py", line 16, in 
> >     servernegotiator = ServerNegotiator(host,HostID, port, key)
> >   File "/home/twistedphrame/Desktop/communication/
> > ServerNegotiator.py", line 188, in __init__
> >     self.server = SocketServer.TCPServer((host, port), negotiator)
> >   File "/usr/lib/python2.6/SocketServer.py", line 400, in __init__
> >     self.server_bind()
> >   File "/usr/lib/python2.6/SocketServer.py", line 411, in server_bind
> >     self.socket.bind(self.server_address)
> >   File "", line 1, in bind
> > TypeError: an integer is required
>
> servernegotiator = ServerNegotiator(host,HostID, port, key)
> class ServerNegotiator:
>     def __init__(self, host, port, hostid, rsa_key, buf = 512):
>
> you swapped port & hostID in your call
>
> JM

tThanks guys it's working now... feel a little stupid though.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pycrypto RSA Issue

2010-02-09 Thread Jordan Apgar
On Feb 9, 1:27 pm, Legrandin 
wrote:
> > gkey = RSA.generate(384, Random.new().read)
> > string = str((gkey.publickey().__getstate__(),(333,444)))
>
> You are encrypting with RSA a piece of data which is way
> larger than the key size (48 bytes).

ah thanks Legrandin
-- 
http://mail.python.org/mailman/listinfo/python-list


SocketServer error: AttributeError: instance has no __call__ method

2010-02-10 Thread Jordan Apgar
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.dump

SimpleXMLRPCServer and client address

2010-02-10 Thread Jordan Apgar
I'm trying to right a server that needs specific information for each
client accessing it.  The easiest way I could think of doing this is
keeping this information based on ip address (the information is only
valid for a short time).  I know there is no was to get the client's
address directly and have seen a few examples of subclassing the
SimpleXMLRPCServer like this:

class RPCServer(SimpleXMLRPCServer):

def _dispatch(self, method, params):
"""Extend dispatch, adding client info to some parameters."""
if method in ({my list of methods I needed client address}):
return SimpleXMLRPCServer._dispatch(self, method,
params+(self.client_address,))
return SimpleXMLRPCServer._dispatch(self, method, params);


but to be honest I don't understand what's going on there or how to
use it.  Would anyone be able to explain this to me?
-- 
http://mail.python.org/mailman/listinfo/python-list


Pycrypto RSA ciphertext to string back to ciphertext issue

2010-02-11 Thread Jordan Apgar
Hey all,
I'm trying to convert the encrypted data from RSA to a string for
sending over xmlrpc and then back to usable data.  Whenever I decrypt
I just get junk data.  Has anyone else tried doing this?  Here's some
example code:

from Crypto.PublicKey import RSA
from Crypto import Random

key = RSA.generate(384, Random.new().read)
l = str(key.encrypt("dog",""))
l = stringToTuple(l)
l = key.decrypt(tuple(l))
print l

string to tuple:
def stringToTuple(string):
if string[0] + string[-1] == "()":
items = string[1:-1]
items = items.split(',')
return items
else:
raise ValueError("Badly formatted string (missing brackets).")

thanks for any help, I've been messing with this for days and have
come up dry.
-- 
http://mail.python.org/mailman/listinfo/python-list


fork vs threading.Thread

2010-02-12 Thread Jordan Apgar
I'm trying to run two servers in the same program at once.  Here are
the two:
class TftpServJ(Thread):
def __init__(self, ip, root, port=69, debug = False ):
Thread.__init__(self)
setup stuff here

def run(self):
try:
self.server.listen(self.ip, self.port)
except KeyboardInterrupt:
pass

and
class XMLServer(Thread):
def __init__(self, host, port, hostid, rsa_key):
Thread.__init__(self)
 setup stuff

def run(self):
self.server.serve_forever()


I call them as:
tftpserv = TftpServJ(host, "/home/twistedphrame/Desktop/xmlrpc_server/
server")
tftpserv.run()
xmlserv = XMLServer(host, port, HostID, key)
xmlserv.run()


it seems that tftpserv runs but wont go on to spawn xmlserv as well.
do I need to fork if I want both these to run at the same time?  It
was my impression that by using Thread execution in the main program
would continue.



-- 
http://mail.python.org/mailman/listinfo/python-list


Binary data transfer issue

2010-03-15 Thread Jordan Apgar
Hi all,

I'm trying to transfer a binary file over xmlrpclib.   My test file is
a .jpeg file.  I can transfer all the data over but when I go to open
the .jpeg I get "Error interpreting JPEG image file (Invalid JPEG file
structure: SOS before SOF)"

here's the code:

===Various Shared Functions
#EncodeAES takes a Cipher c and encrypts a String s
EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))

#DecodeAES takes a Cipher C and decrypts the cyphertext e
#then removes the padding from the decrypted string
DecodeAES = lambda c, e:
c.decrypt(base64.b64decode(e)).rstrip(PADDING)


CIPHER = AES.new("KEY")


# method to change strings back into tuples
def stringToTuple(string):
if string[0] + string[-1] == "()":
items = string[1:-1]
items = items.split(',')
return items
else:
raise ValueError("Badly formatted string (missing brackets).")

==client=
 def Download(self, FileName, Data_Size=1024):
ErrorCount = 0
Pkt_Num = -1
OldPkt_Num = Pkt_Num
localFile = open("files/"+FileName, "wb")
print
print "Requesting First Packet"
packet = self.client.download(EncodeAES(CIPHER, str(Pkt_Num)))
if packet[0] == "False":
print packet[1]
return False
packet = stringToTuple(DecodeAES(CIPHER, packet))
if int(packet[0]) == -3:
print "Received Error Packet"
else:
print "Received Packet: ", int(packet[0])

print "packet[1], ", packet[2:-1]

Pkt_Num = int(packet[0])

while Pkt_Num >= 0 and ErrorCount <= 10:
if (OldPkt_Num +1) == Pkt_Num:
OldPkt_Num = OldPkt_Num +1
localFile.write(binascii.
a2b_base64(packet[1][2:-1]))
#<<<=
os.path.getsize("files/"+self.usr_dict[client][FILENAME]):
return EncodeAES(CIPHER,
 str((-2, "File Transfer Complete")))
try:
f = open("files/"+self.usr_dict[client][FILENAME],
"rb")
except IOError:
   return EncodeAES(CIPHER,
 str((-3, "File Could Not be opened")))
data = f.read((Pkt_Num+1 * DataRate))
data = None
data = binascii.b2a_base64(f.read(DataRate))
#<<

datetime string conversion error

2010-03-16 Thread Jordan Apgar
Hey all,
I'm trying to convert a string to a date time object and all my fields
convert except for month which seems to default to january.

here's what I'm doing:
date = "2010-03-16 14:46:38.409137"
 olddate = datetime.strptime(date,"%Y-%m-%j %H:%M:%S.%f")

print date
print olddate

I get:
2010-03-16 14:46:38.409137
2010-01-16 14:46:38.409137

notice the 01 in the second date from what I could tell everything is
formatted correctly.

thanks for the help.
~Jordan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: datetime string conversion error

2010-03-16 Thread Jordan Apgar
On Mar 16, 3:07 pm, Christian Heimes  wrote:
> Jordan Apgar wrote:
> > Hey all,
> > I'm trying to convert a string to a date time object and all my fields
> > convert except for month which seems to default to january.
>
> > here's what I'm doing:
> > date = "2010-03-16 14:46:38.409137"
> >  olddate = datetime.strptime(date,"%Y-%m-%j %H:%M:%S.%f")
>
> > print date
> > print olddate
>
> > I get:
> > 2010-03-16 14:46:38.409137
> > 2010-01-16 14:46:38.409137
>
> > notice the 01 in the second date from what I could tell everything is
> > formatted correctly.
>
> %j is documtend as "Day of the year as a decimal number [001,366].". Did
> you mean %d instead?
>
> Christian

That fixed it, thank you

-- 
http://mail.python.org/mailman/listinfo/python-list