Code

conf2xml: Added HTTP POST support
[nagixsc.git] / nagixsc.py
index 191d26e2f0a1e90b9f6197f450a8c2f53cc61ac4..56c4cf3ead117ca047a818085912232a5ad514d4 100644 (file)
@@ -4,6 +4,7 @@ import SocketServer
 import base64
 import datetime
 import libxml2
+import mimetools
 import os
 import random
 import shlex
@@ -386,6 +387,28 @@ def reset_future_timestamp(timestamp, now):
 
 ##############################################################################
 
+def encode_multipart(xmldoc, httpuser, httppasswd):
+       BOUNDARY = mimetools.choose_boundary()
+       CRLF = '\r\n'
+       L = []
+       L.append('--' + BOUNDARY)
+       L.append('Content-Disposition: form-data; name="xmlfile"; filename="xmlfile"')
+       L.append('Content-Type: application/xml')
+       L.append('')
+       L.append(xmldoc.serialize())
+       L.append('--' + BOUNDARY + '--')
+       L.append('')
+       body = CRLF.join(L)
+       content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
+       headers = {'Content-Type': content_type, 'Content-Length': str(len(body))}
+
+       if httpuser and httppasswd:
+               headers['Authorization'] = 'Basic %s' % base64.b64encode(':'.join([httpuser, httppasswd]))
+
+       return (headers, body)
+
+##############################################################################
+
 class MyHTTPServer(BaseHTTPServer.HTTPServer):
        def __init__(self, server_address, HandlerClass, ssl=False, sslpemfile=None):
                if ssl: