title: kerberos authentication in python
body: <p>we have a database running MSSQL 2008 and I am using below Python (2.7) code to authenticate using Kerberos.</p>
  
  <pre><code>import urllib2
  from urlparse import urlparse
  from socket import gethostbyaddr  
  from sspi import ClientAuth
  from base64 import b64encode, b64decode
  
  
  def getService(url):
      return 'host/{}'.format(
          gethostbyaddr(
              urlparse(url).netloc.split(':')[0]
          )[0][:-16]
      )
  
  status, buff = 1, None
  c = ClientAuth('Kerberos', targetspn=getService(url))
  while status != 0:
      status, buff = c.authorize(buff)
  
      tgt = b64encode(buff[0].Buffer)
      authorization_header = \
                  ("Negotiate " + tgt)
  </code></pre>
  
  <p>When I try this code in python console, getService can resolve the address correctly giving me </p>
  
  <blockquote>
    <p>host/SQLSERVER1</p>
  </blockquote>
  
  <p>but when I do status, buff = c.authorize(buff) it gives me </p>
  
  <blockquote>
    <p>Traceback (most recent call last):   File "", line 1, in
       File "C:\python27\lib\site-packages\win32\lib\sspi.py",
    line 139, in authorize
        sec_buffer_out) pywintypes.error: (-2146893053, 'InitializeSecurityContext', 'The specified target is unknown or
    unreachable')</p>
  </blockquote>
  
  <p>Why does it say The specified target is unknown or unreachable? when I can ping it and gethostbyaddr as well ? is there anyway to resolve this ? </p>