Monday, June 25, 2012

Dumping an WSDL file

To dump/download an wsdl file including all its imported wsdl and xsd files, you can use the svcutil tool available in the Microsoft SDK.

How to use:

  • 1. open a command prompt
  • 2. change directory to the windows SDK bin directory: cd "C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin"
  • 3. type the following command in the prompt: svcutil /t:metadata /directory:D:\some-directory http://your-wsdl-url.wsdl

The result is all the files will be dumped to the D:\some-directory folder, all the unnecessary things (like comments) will be skipped, and you will get a clean wsdl.

Example dumping ONVIF deviceio.wsdl from onvif.org: svcutil /t:metadata /directory:D:\onvif http://www.onvif.org/onvif/ver10/deviceio.wsdl

The result:

Have a nice day.

Friday, June 22, 2012

Python: HTTP Basic authentication with httplib

You can check how http basic authentication can be requested by the server from the user using php in this post.

To make an HTTP request that needs HTTP basic authentication you need to base64 encode the username and password like : base64encode(username + ':' + password) and add it to an HTTP header:

import base64
import string
auth = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
webservice.putheader("Authorization", "Basic %s" % auth)

Example usage with httplib:
import httplib
import base64
import string

host = "host.or.ip"
url = "/url"
username = 'user'
password = 'pass'
message = 'some message'

# base64 encode the username and password
auth = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')

webservice = httplib.HTTP(host)
# write your headers
webservice.putrequest("POST", url)
webservice.putheader("Host", host)
webservice.putheader("User-Agent", "Python http auth")
webservice.putheader("Content-type", "text/html; charset=\"UTF-8\"")
webservice.putheader("Content-length", "%d" % len(message))
# write the Authorization header like: 'Basic base64encode(username + ':' + password)
webservice.putheader("Authorization", "Basic %s" % auth)

webservice.endheaders()
webservice.send(message)
# get the response
statuscode, statusmessage, header = webservice.getreply()
print "Response: ", statuscode, statusmessage
print "Headers: ", header
res = webservice.getfile().read()
print 'Content: ', res