example of using urllib2 with https urls

M

Michele Simionato

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
 
L

Larry Bates

<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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top