Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Python, HTTPS (SSL), tlslite and POST method (and lots of pain)

138 views
Skip to first unread message
Message has been deleted

yat...@gmail.com

unread,
Feb 23, 2009, 4:17:31 PM2/23/09
to
Hi there.
Since quite long time I'm trying to achieve following things:
- for some sort of testing I need webserver with https access
- with POST method implemented

I found something which fits it in nicely written in Python,
connected
my toys and... server hangs in some weird "inter-state" while serving
POST method

Below server code:

-------------------------

#!/usr/bin/python

from SocketServer import *
from BaseHTTPServer import *
from SimpleHTTPServer import *
from tlslite.api import *
import string,cgi,time
from os import curdir, sep

s = open("./server.pem").read()
x509 = X509()
x509.parse(s)
certChain = X509CertChain([x509])

s = open("./server.pem").read()
privateKey = parsePEMKey(s, private=True)

sessionCache = SessionCache()

#class MyHandler(BaseHTTPRequestHandler):
class MyHandler(SimpleHTTPRequestHandler):

def do_POST(self):
global rootnode
try:
ctype, pdict = cgi.parse_header(self.headers.getheader
('content-type'))
form = cgi.FieldStorage(fp = self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers
['content-type']} )

if ctype == 'multipart/form-data':

if form.has_key('postdata'):

temp_file = open(form['postdata'].filename,'wb')
buffer = form['postdata'].file.read()
temp_file.write(buffer)
temp_file.close()

self.send_response(200)
#set apriopriate header if you want send
something
#self.send_header('Content-type', 'text/plain')
self.send_header('Content-type', 'text/html')
self.end_headers()

#... and send it
#self.wfile.write('OK')
self.wfile.write('<html><body>Post OK.</body></
html>')
except :
pass

class MyHTTPServer(ThreadingMixIn, TLSSocketServerMixIn, HTTPServer):
def handshake(self, tlsConnection):
try:
tlsConnection.handshakeServer(certChain=certChain,
privateKey=privateKey,
sessionCache=sessionCache)
tlsConnection.ignoreAbruptClose = True
return True
except TLSError, error:
print "Handshake failure:", str(error)
return False

httpd = MyHTTPServer(('127.0.0.1', 443), MyHandler)
#httpd = HTTPServer(('127.0.0.1', 80), MyHandler)

httpd.serve_forever()

--------------------

And page which is used to serve POST request:

<html>
<body>
<form method='POST' enctype='multipart/form-data'
action='./'>

File to post: <input type=file name=postdata><br><br>
<input type=submit value=Send>
</form>
</body>
</html>

File is being nicely send to server but as I've mentioned above
server
is halted in some interstate. Only after Ctrl+C terminates it
confirmation of successful transmition is showed in web browser

Choosing to use server without SSL (HTTPServer instead of
MyHTTPServer) makes everything work without glitch.

I would be happy to hear any suggestions

If somebody knows any alternative to achieve similar thing (https +
POST method on server side) it would be enough to get my tests
working.

Thx in advance

Greetz
Yatsek

---
sorry for repost - I've made mistake and tried correct it just after
posting by deleting it. If it confuses somebody then again - sorry for
that

yat...@gmail.com

unread,
Feb 24, 2009, 5:48:10 AM2/24/09
to
OK I found workaround.

So just for other fighting the same thing:
Instead of using tlslite use pyOpenSSL and as a base for your server
you can use:
http://code.activestate.com/recipes/442473/

When you add do_POST method everything works

greetz
Jacek

0 new messages