Desidero prelevare alcuni dati da una pagina Web che richiede il nome utente e la password di Windows.Autenticazione di Windows con Python e urllib2
Finora, ho:
opener = build_opener()
try:
page = opener.open("http://somepagewhichneedsmywindowsusernameandpassword/")
print page
except URLError:
print "Oh noes."
È questo supportato da urllib2? Ho trovato Python NTLM, ma questo richiede che inserisca il mio nome utente e la mia password. Esiste un modo per afferrare le informazioni di autenticazione in qualche modo (ad es. Come IE o Firefox, se ho modificato le impostazioni network.automatic-ntlm-auth.trusted-uris
).
Modifica dopo la risposta di msander
Così ora ho ottenuto questo:
# Send a simple "message" over a socket - send the number of bytes first,
# then the string. Ditto for receive.
def _send_msg(s, m):
s.send(struct.pack("i", len(m)))
s.send(m)
def _get_msg(s):
size_data = s.recv(struct.calcsize("i"))
if not size_data:
return None
cb = struct.unpack("i", size_data)[0]
return s.recv(cb)
def sspi_client():
c = httplib.HTTPConnection("myserver")
c.connect()
# Do the auth dance.
ca = sspi.ClientAuth("NTLM", win32api.GetUserName())
data = None
while 1:
err, out_buf = ca.authorize(data) # error 400 triggered by this line
_send_msg(c.sock, out_buf[0].Buffer)
if err==0:
break
data = _get_msg(c.sock)
print "Auth dance complete - sending a few encryted messages"
# Assume out data is sensitive - encrypt the message.
for data in "Hello from the client".split():
blob, key = ca.encrypt(data)
_send_msg(c.sock, blob)
_send_msg(c.sock, key)
c.sock.close()
print "Client completed."
che è abbastanza ben strappato dal socket_server.py
(vedi here). Ma ottengo un errore 400 - cattiva richiesta. Qualcuno ha altre idee?
Grazie,
Dom
"prendi le informazioni di autenticazione" intendi, prendi il tuo nome di dominio e la password da qualche parte e usali nel tuo script? Mi piacerebbe sapere se è possibile anche :) – kender
@kender - Sì, è esattamente quello che intendo. –
Forse le risposte a [la mia domanda] (http://stackoverflow.com/questions/356321/scripted-files-download-under-windows) ti aiuteranno. – kender