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

  • SEARCH
  • Home
  • 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 1074939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:12:17+00:00 2026-05-16T21:12:17+00:00

I have the following simple Python code that makes a simple post request to

  • 0

I have the following simple Python code that makes a simple post request to a REST service –

params= { "param1" : param1,
          "param2" : param2,
          "param3" : param3 }
xmlResults = urllib.urlopen(MY_APP_PATH, urllib.urlencode(params)).read()
results = MyResponseParser.parse(xmlResults)

The problem is that the url used to call the REST service will now require basic authentication (username and password). How can I incorporate a username and password / basic authentication into this code, as simply as possible?

  • 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. Editorial Team
    Editorial Team
    2026-05-16T21:12:17+00:00Added an answer on May 16, 2026 at 9:12 pm

    If basic authentication = HTTP authentication, use this:

    import urllib
    import urllib2
    
    username = 'foo'
    password = 'bar'
    
    passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
    passman.add_password(None, MY_APP_PATH, username, password)
    authhandler = urllib2.HTTPBasicAuthHandler(passman)
    opener = urllib2.build_opener(authhandler)
    urllib2.install_opener(opener)
    
    params= { "param1" : param1,
              "param2" : param2,
              "param3" : param3 }
    
    xmlResults = urllib2.urlopen(MY_APP_PATH, urllib.urlencode(params)).read()
    results = MyResponseParser.parse(xmlResults)
    

    If not, use mechanize or cookielib to make an additional request for logging in. But if the service you access has an XML API, this API surely includes auth too.

    2016 edit: By all means, use the requests library! It provides all of the above in a single call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following simple python test script that uses Suds to call a
I have the following simple methods for writing a python object to a file
I have a following simple code: def get(): return [lambda: i for i in
I have the following query that I'm executing using a Python script (by using
Let's say we have the following mega-simple Python script: print Initializing.... a=10 print Variable
I have a very simple piece of code that I used in previous versions
Say that I have the Python code: someString = file(filename.jpg).read() How can I replicate
I have a python question that I'm sure is pretty simple - please feel
I have some python code that may result in a division by 0, but
I have a simple python script that works fine on Linux, I moved it

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.