0

I am trying to create a very basic app that will be able to connect to a web server which host my college assignments, results, and more and notify me when ever there's something new on it. Currently I am trying to get the hang of the requests module, but I am not able to login as the server uses this kind of authentication, and it gives me error 401 unauthorized.

I tried searching how to authenticate to web servers tried using sockets, with no luck. Could you please help me find out how to do this?

EDIT: I am using python 3.4

3 Answers 3

3

After inspecting the headers in the response for that URL, I think the server is trying to use NTLM authentication.

Try installing requests-ntlm (e.g. with pip install requests_ntlm) and then doing this:

import requests
from requests_ntlm import HttpNtlmAuth

requests.get('http://moodle.mcast.edu.mt:8085/',
    auth=HttpNtlmAuth('domain\\username', 'password'))
Sign up to request clarification or add additional context in comments.

7 Comments

still prompts the login form. Does not login.
I'm fairly certain that this should work. Does your user name or password contain any characters that might need to be specially-encoded (for now we can look for anything that's not a letter and not a number).
i retried couple of minutes ago this time i used chrome and it authenticated me succefully. so how do i got about doing that in python? connect using requests and providing the host name with this http://<user>:<pass>@moodle.mcast.edu.mt:8085 signature?
@EvgenyDanilenko, I did a bit more digging. It looks like this site actually uses NTLM authentication. I have updated my answer with this in mind.
yea i was wondering what was the NTML in Authorization request. I am trying to install the module. I think this should be it tho
|
1

You need to attach a simple authentication header within the socket request headers.

Example;

import base64

mySocket.send('GET / HTTP/1.1\r\nAuthorization: Basic %s\r\n\r\n' % base64.b64encode('user:pass'))

Python 3x;

import base64

template = 'GET / HTTP/1.1\r\nAuthorization: Basic %s\r\n\r\n'
mySocket.send(bytes(template % base64.b64encode(bytes('user:pass', 'UTF-8')), 'UTF-8'))

7 Comments

i tried but i still get this error TypeError: 'str' does not support the buffer interface. i also had changed the last part to base64.b64encode(bytes('u:p', encoding='utf-8')) still get that error
Edited the initial solution and added the python 3x way, I've just tested it on my workspace and it worked fine with no type errors.
yea im still on it. trying to understand what BrokenPipeError: [Errno 32] Broken pipe means
Error 32 (BrokenPipeError) gets thrown when the other side fully closed connection and you get a SIGPIPE message. (usually you ignore it w/ try-except)
nvm that, i had forgot to connect to the server. dealing with other stuff here at the same time. but uh i got this err b'HTTP/1.1 400 Bad Request\r\nContent-Type: text/html; charset=us-ascii\r\nServer: Microsoft-HTTPAPI/2.0\r\n'
|
0

They might not supply a programmatic API with which to authorize requests. If that is the case, you could try using selenium to manually open a browser and fill the details in for you. Selenium has handling for alert boxes too apparently, though I haven't used it myself.

2 Comments

I looked at the link you posted, and I don't think that, that would work, as it is "searching the element by id" and pressing f12 to see website info does not share any elements besides that frame.
Yeah you're right; I hadn't realised it was a pop-up box type thing (I thought it was an html form from the screenshot)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.