example of using urllib2 with https urls

Discussion in 'Python' started by Michele Simionato, Oct 18, 2005.

  1. Can somebody provide an example of how to retrieve a https url, given
    username and password? I don't find it in the standard documentation.
    TIA,

    Michele Simionato
    Michele Simionato, Oct 18, 2005
    #1
    1. Advertising

  2. Michele Simionato

    Larry Bates Guest

    <snipped from working code to upload a file to https:
    site---WARNING not tested after snipping>

    import httplib
    import base64
    import sys
    import random

    #
    # Get the length of the file from os.stat
    #
    username='<username>'
    password='<password>'
    file='<path to file to be uploaded>'
    size=os.stat(file)[6]
    #
    # file contains the entire path, split off the name
    # WebSafe.
    #
    name=os.path.basename(file)

    url='https://www.somedomain.com'
    auth_string = base64.encodestring('%s:%s' % (username, password))
    rid = '%02x' % random.uniform(0, sys.maxint-1)

    conn = httplib.HTTP(url)
    conn.putrequest('PUT', '%s/%s' % (path, rid))
    conn.putheader('Content-Type', 'text/plain')
    conn.putheader('Content-Length', str(size))
    conn.putheader('Authorization', 'Basic %s' % auth_string)
    conn.endheaders()

    #
    # Open file in binary mode for reading
    #
    fp=open(file, 'rb')
    #
    # Loop over all the file's blocks and send them sequentially
    #
    blocknum=0
    while 1:
    bodypart=fp.read(blocksize)
    blocknum+=1
    if blocknum % 10 == 0:
    print "upload-sending blocknum=", blocknum

    if not bodypart: break
    conn.send(bodypart)

    fp.close()
    reply, msg, headers = conn.getreply()
    print "upload-PUT reply=", reply, " msg=", msg, "headers=", headers


    <end snip>

    This is for Basic Authentication (if your https site is using
    something different, method would be different). May not be what
    you need. Hope this helps.

    Larry Bates

    Michele Simionato wrote:
    > Can somebody provide an example of how to retrieve a https url, given
    > username and password? I don't find it in the standard documentation.
    > TIA,
    >
    > Michele Simionato
    >
    Larry Bates, Oct 18, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kaidi
    Replies:
    5
    Views:
    466
    Andrew Thompson
    Jan 4, 2004
  2. Anand Pillai

    Making urllib2 work with proxy urls

    Anand Pillai, Apr 13, 2004, in forum: Python
    Replies:
    2
    Views:
    936
  3. Nathan Sokalski

    Converting Relative URLs into Absolute URLs

    Nathan Sokalski, Aug 11, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    741
    Sriram Srivatsan
    Aug 12, 2008
  4. Adam Monsen

    JDBC URLs ...not really URLs?

    Adam Monsen, Feb 6, 2009, in forum: Java
    Replies:
    11
    Views:
    6,201
    Adam Monsen
    Feb 8, 2009
  5. Steve T.

    dynamic URLS convert to static URLS for search engines

    Steve T., Mar 1, 2004, in forum: ASP .Net Web Services
    Replies:
    7
    Views:
    286
    Steve T.
    Mar 4, 2004
Loading...

Share This Page