Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 96417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:48:41+00:00 2026-05-10T23:48:41+00:00

I have a script that I’d like to continue using, but it looks like

  • 0

I have a script that I’d like to continue using, but it looks like I either have to find some workaround for a bug in Python 3, or downgrade back to 2.6, and thus having to downgrade other scripts as well…

Hopefully someone here have already managed to find a workaround.

The problem is that due to the new changes in Python 3.0 regarding bytes and strings, not all the library code is apparently tested.

I have a script that downloades a page from a web server. This script passed a username and password as part of the url in python 2.6, but in Python 3.0, this doesn’t work any more.

For instance, this:

import urllib.request; url = 'http://username:password@server/file'; urllib.request.urlretrieve(url, 'temp.dat'); 

fails with this exception:

Traceback (most recent call last):   File 'C:\Temp\test.py', line 5, in <module>     urllib.request.urlretrieve(url, 'test.html');   File 'C:\Python30\lib\urllib\request.py', line 134, in urlretrieve     return _urlopener.retrieve(url, filename, reporthook, data)   File 'C:\Python30\lib\urllib\request.py', line 1476, in retrieve     fp = self.open(url, data)   File 'C:\Python30\lib\urllib\request.py', line 1444, in open     return getattr(self, name)(url)   File 'C:\Python30\lib\urllib\request.py', line 1618, in open_http     return self._open_generic_http(http.client.HTTPConnection, url, data)   File 'C:\Python30\lib\urllib\request.py', line 1576, in _open_generic_http     auth = base64.b64encode(user_passwd).strip()   File 'C:\Python30\lib\base64.py', line 56, in b64encode     raise TypeError('expected bytes, not %s' % s.__class__.__name__) TypeError: expected bytes, not str 

Apparently, base64-encoding now needs bytes in and outputs a string, and thus urlretrieve (or some code therein) which builds up a string of username:password, and tries to base64-encode this for simple authorization, fails.

If I instead try to use urlopen, like this:

import urllib.request; url = 'http://username:password@server/file'; f = urllib.request.urlopen(url); contents = f.read(); 

Then it fails with this exception:

Traceback (most recent call last):   File 'C:\Temp\test.py', line 5, in <module>     f = urllib.request.urlopen(url);   File 'C:\Python30\lib\urllib\request.py', line 122, in urlopen     return _opener.open(url, data, timeout)   File 'C:\Python30\lib\urllib\request.py', line 359, in open     response = self._open(req, data)   File 'C:\Python30\lib\urllib\request.py', line 377, in _open     '_open', req)   File 'C:\Python30\lib\urllib\request.py', line 337, in _call_chain     result = func(*args)   File 'C:\Python30\lib\urllib\request.py', line 1082, in http_open     return self.do_open(http.client.HTTPConnection, req)   File 'C:\Python30\lib\urllib\request.py', line 1051, in do_open     h = http_class(host, timeout=req.timeout) # will parse host:port   File 'C:\Python30\lib\http\client.py', line 620, in __init__     self._set_hostport(host, port)   File 'C:\Python30\lib\http\client.py', line 632, in _set_hostport     raise InvalidURL('nonnumeric port: '%s'' % host[i+1:]) http.client.InvalidURL: nonnumeric port: 'password@server' 

Apparently the url parsing in this ‘next gen url retrieval library’ doesn’t know what to do with username and passwords in the url.

What other choices do I have?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:48:42+00:00Added an answer on May 10, 2026 at 11:48 pm

    Direct from the Py3k docs: http://docs.python.org/dev/py3k/library/urllib.request.html#examples

    import urllib.request # Create an OpenerDirector with support for Basic HTTP Authentication... auth_handler = urllib.request.HTTPBasicAuthHandler() auth_handler.add_password(realm='PDQ Application',                           uri='https://mahler:8092/site-updates.py',                           user='klem',                           passwd='kadidd!ehopper') opener = urllib.request.build_opener(auth_handler) # ...and install it globally so it can be used with urlopen. urllib.request.install_opener(opener) urllib.request.urlopen('http://www.example.com/login.html') 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that checks responses from HTTP servers using the PEAR HTTP
I have a script that works fine on my test server (using IIS6). The
Have a script that using jquery validation. Tested on 2 Blackberry's and neither worked
I have a script that runs on some pages. The problem is that I
I have this script that I'd like to in addition save as a xls
I have a script that loads between ~5000-8000 objects, performs some logic on them
I have a script that is using the google charts API and the gChart
I have a script that works great, but I need the background color to
I have a script that appends some rows to a table. One of the
I have a script that parses the filenames of TV episodes (show.name.s01e02.avi for example),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.