Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use urllib with username/password authentication in python 3?

Here is my problem with urllib in python 3.

I wrote a piece of code which works well in Python 2.7 and is using urllib2. It goes to the page on Internet (which requires authorization) and grabs me the info from that page.

The real problem for me is that I can't make my code working in python 3.4 because there is no urllib2, and urllib works differently; even after few hours of googling and reading I got nothing. So if somebody can help me to solve this, I'd really appreciate that help.

Here is my code:

    request = urllib2.Request('http://mysite/admin/index.cgi?index=127')     base64string = base64.encodestring('%s:%s' % ('login', 'password')).replace('\n', '')     request.add_header("Authorization", "Basic %s" % base64string)     result = urllib2.urlopen(request)     resulttext = result.read() 
like image 864
mooose Avatar asked Jul 08 '14 14:07

mooose


People also ask

How do I authenticate a URL in Python?

Since, everyone can't be allowed to access data from every URL, one would require authentication primarily. To achieve this authentication, typically one provides authentication data through Authorization header or a custom header defined by server. Replace “user” and “pass” with your username and password.

Is Urllib built in Python 3?

The urllib module in Python 3 allows you access websites via your program. This opens up as many doors for your programs as the internet opens up for you. urllib in Python 3 is slightly different than urllib2 in Python 2, but they are mostly the same.


2 Answers

Thankfully to you guys I finally figured out the way it works. Here is my code:

request = urllib.request.Request('http://mysite/admin/index.cgi?index=127') base64string = base64.b64encode(bytes('%s:%s' % ('login', 'password'),'ascii')) request.add_header("Authorization", "Basic %s" % base64string.decode('utf-8')) result = urllib.request.urlopen(request) resulttext = result.read() 

After all, there is one more difference with urllib: the resulttext variable in my case had the type of <bytes> instead of <str>, so to do something with text inside it I had to decode it:

text = resulttext.decode(encoding='utf-8',errors='ignore') 
like image 171
mooose Avatar answered Oct 18 '22 05:10

mooose


What about urllib.request ? It seems it has everything you need.

import base64 import urllib.request  request = urllib.request.Request('http://mysite/admin/index.cgi?index=127') base64string =  bytes('%s:%s' % ('login', 'password'), 'ascii') request.add_header("Authorization", "Basic %s" % base64string) result = urllib.request.urlopen(request) resulttext = result.read() 
like image 28
Seb D. Avatar answered Oct 18 '22 04:10

Seb D.