Amyzz 0 Newbie Poster

Using python 2.4.3 I'm trying to make a HTTP server which: (1) receives a message with 'POST', (2) makes an answer message, (3) send the answer message back to sender.

Step (1) and (2) go well, Step 3 fails.... sometimes. It looks like there is some threading issue with the used httplib2.Http() thing. The output when it fails is:

Unexpected error: socket.error
10.96.5.127 - - [26/May/2014 07:55:39] code 500, message Error processing in do_POST
10.96.5.127 - - [26/May/2014 07:55:39] "POST /exchange/PERFOR HTTP/1.1" 500 -

Here is my code (import are not shown) :

(Indentation is not the issue (failing is not constant))

 class PostHandler(BaseHTTPRequestHandler):
        def do_POST(self):
            try:
                # (1) Message from sender is received by server : OK
                content_len = int(self.headers.getheader('content-length'))
                msg = self.rfile.read(content_len)
                self.send_response(200)
                self.end_headers()
                # (2) Received message is used to make the 'smsg' message to be send back.
                # This goes OK so I don't need to show the code.
                # (3) Now 'smsg' shoud be send back to sender
                headers = {"Content-type" : "text/xml" , "Accept": "text/xml" , "User-Agent" :  "haboob/5.9.0.0.4 build-4857" , "SOAPAction": "\"ebXML\"" }
                url = 'http://mynode.local:4080/destination'
                http = httplib2.Http()
                response, content = http.request(url, method="POST", headers=headers, body=smsg)
            except :
                print "Unexpected error:", sys.exc_info()[0]
                self.send_error(500,'Error processing in do_POST')
            return

    class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
        """Handle requests in a separate thread."""

    if __name__ == '__main__':
        try:
            port=4080
            server = ThreadedHTTPServer(('',port), PostHandler) 
            server.request_queue_size=60
            server.serve_forever()
        except KeyboardInterrupt:
            print ('^C received, shutting down server')
            server.socket.close()

Any help will be very welcome!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.