From: brian z. <bz...@zi...> - 2002-05-08 12:36:08
|
Tyson, Here's a snippet I have in my local socket.py to facilitate SSL: ************************************************************************ ********************* try: import javax.net.ssl.SSLSocketFactory provider = com.sun.net.ssl.internal.ssl.Provider() java.security.Security.addProvider(provider) sslerror = IOError class SSLObject: def __init__(self, sock): self.sock = sock self.istream = self.sock.getInputStream() self.ostream = self.sock.getOutputStream() def read(self, n=0): assert self.sock if n: data = jarray.zeros(n, 'b') m = self.istream.read(data) if m <= 0: return "" if m < n: data = data[:m] return data.tostring() else: cnt = 0 data = jarray.zeros(128, 'b') while 1: m = self.istream.read() if m <= 0: break size = len(data) if cnt >= size: tmp = data data = jarray.zeros(size + 128, 'b') java.lang.System.arraycopy(tmp, 0, data, 0, size) data[cnt] = m cnt += 1 if cnt == 0: return "" else: return data[:cnt].tostring() def write(self, s): assert self.sock try: n = len(s) self.ostream.write(s) return n except java.net.SocketException, e: #print "tried to send [%s]" % (s) #raise error(errno.EINTR, e.getMessage()) raise except java.io.IOException, e: #raise error(errno.EINTR, e.getMessage()) raise def ssl(socket, keyfile=None, certfile=None): assert not socket.server try: factory = javax.net.ssl.SSLSocketFactory.getDefault() hostname, port = socket.getpeername() return SSLObject(factory.createSocket(socket.sock, hostname, port, 1)) except java.net.SocketException, e: raise error(0, e.getMessage()) except: pass ************************************************************************ ********************* It seems to work for connecting to https://sf.net and grabbing the page. It's not all that thoroughly tested so you might find some bugs and such. Let me know if you have any success with it. I am currently working on an implementation of select and non-blocking sockets but I've hit a couple of stumbling blocks, it works for the most part though. I think I might release it soon anyway and I'd add SSL support (written in Java) using the above as a prototype, so any feedback would be great! The source for zxSocket is available through CVS at http://sf.net/projects/zxpy; the project name is zxSocket. The problem I'm having if anyone has read this far is with the ByteBuffer->String conversion required by Jython. If it's just textual data then I have no problem, but I can't get it to work properly with binary data, say downloading an image. I'm no expert on this stuff so if there are any please contact me. thanks, brian > -----Original Message----- > From: jyt...@li... > [mailto:jyt...@li...] On Behalf > Of tw...@3a... > Sent: Monday, May 06, 2002 6:28 PM > To: jyt...@li... > Subject: [Jython-users] SSL connections in Jython > > > I've searched the net for some answers, as well as the list > archives, and > haven't run across any discussions or howto's that cover > using SSL sockets inside Jython. I sifted through the > httplib.py file and noticed references to socket.ssl, which > appears absent from socket.py source file > in 2.1. > > So, my fallback is to install JSSE libs, create > URLConnections per their examples, and continue from there. > I thought I'd ask if anyone has experience using SSL libs in > Jython. If not, no biggie...I'll see if I can't make some > mods to socket.py to support using ssl connections with > httplib. I'd rather not port some of my code that's humming > nicely using > httplib. > > -tyson > > p.s. I've been lurking on the lists for a while, and must > say I absolutely love Python/Jython. We're using it > extensively here, embedding it in some EJB apps, building > command line apps with it, etc. The whole devel team here is > certainly "drinking the kool-aid". Jython/python is esp. nice > for us scheme/lisp fans who are used to having > power and flexibility, and frustrated by the many redundant > lines of code > you have to write in Java. > > > > --------------------------------------------------------- > This message sent using EMUmail -- http://www.emumail.com > --------------------------------------------------------- > > Jumping through hoops to get E-mail on the road? > You've got two choices: Join the circus, or use MollyMail. > > Molly Mail -- http://www.mollymail.com > > > > _______________________________________________________________ > > Have big pipes? SourceForge.net is looking for download > mirrors. We supply the hardware. You get the recognition. > Email Us: ban...@so... > _______________________________________________ > Jython-users mailing list > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-users > |