authentication - Get request with Python 2.7 -


i'm trying send request python 2.7. server i'm trying access has basic authentication , url i'm accessing displaying string want script print string.

so code sending request server , saves string receives server, has basic auth.

the problem prints out html page login page not string.

username = 'username' password = 'password'  server = "http://someserver/update"  def get_auth():     request = urllib2.request(server)     base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')     request.add_header("authorization", "basic %s" % base64string)     result = urllib2.urlopen(request)     # print(result)  def get_string():     f = urllib2.urlopen(server)     print f.read()  def main():     get_auth()     get_string()  if __name__ == '__main__':     main() 

they way using get_string() never sends authentication header.

rather fiddling urllib2, might want try requests library instead::

import requests requests.get('http://someserver/update', auth=('username', 'password')) 

see: http://docs.python-requests.org/en/latest/user/authentication/


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -