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

yatsek at gmail.com yatsek at gmail.com
Mon Feb 23 20:13:46 CET 2009


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



More information about the Python-list mailing list