473,585 Members | 2,657 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

example of using urllib2 with https urls

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

Oct 18 '05 #1
1 4876
<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='<user name>'
password='<pass word>'
file='<path to file to be uploaded>'
size=os.stat(fi le)[6]
#
# file contains the entire path, split off the name
# WebSafe.
#
name=os.path.ba sename(file)

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

conn = httplib.HTTP(ur l)
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.rea d(blocksize)
blocknum+=1
if blocknum % 10 == 0:
print "upload-sending blocknum=", blocknum

if not bodypart: break
conn.send(bodyp art)

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

Oct 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1651
by: Grant Edwards | last post by:
In the program shown below, all of the connections to the servers are using port 443 and https (TLS protocol) -- both the initial connection to login.postini.com and subsequent connections to user-3.postini.com. It works, and I don't mind that it's encrypting the sessions as well as the login, but I'd like understand why the connections to...
7
9273
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. # No warranty express or implied for the accuracy, fitness to purpose
0
1228
by: Fuzzyman | last post by:
Hello all (well, some of you I guess), Is it true that you can't configure a proxy handler for urllib2 to fetch https URLs through a proxy ? I know that you pass a dictionary, keyed by protocol, to the proxy handler - but when we get an exception when we fetch an https URL. Fetching the same URL, through the same proxy, with a browser...
11
3559
by: Johnny Lee | last post by:
Hi, I was using urllib to grab urls from web. here is the work flow of my program: 1. Get base url and max number of urls from user 2. Call filter to validate the base url 3. Read the source of the base url and grab all the urls from "href" property of "a" tag 4. Call filter to validate every url grabbed 5. Continue 3-4 until the number...
1
2821
by: dmbkiwi | last post by:
I've been using urllib2 to try and automate logging into the google adsense page. I want to download the csv report files, so that I can do some analysis of them. However, I don't really know how web forms work, and the examples on the python.org/doc site aren't really helpful. I've found working scripted login code using javascript, but...
4
6682
by: Bo Yang | last post by:
Hi , Recently I use python's urllib2 write a small script to login our university gateway . Usually , I must login into the gateway in order to surf the web . So , every time I start my computer , it is my first thing to do that open a browser to login the gateway ! So , I decide to write such a script , sending some post information to...
3
1491
by: jnair | last post by:
Hi , I am using python2.4 "urllib2" and "cookelib". In line "5" below i provide my credentials to login into a web site.During the first attempt i "fail", judging from the output of line "6". I try again and the second time i succeed,judging from the output of line "8". Now using the "twill" module...
1
1578
by: jnair | last post by:
Hi , I am using python2.4 "urllib2" and "cookelib". In line "5" below i provide my credentials to login into a web site.During the first attempt i "fail", judging from the output of line "6". I try again and the second time i succeed,judging from the output of line "8". Now using the "twill" module...
3
3075
by: itay_k | last post by:
Hi, I am running the following simple code (just open connection to some https page with proxy): proxy= '666.179.227.666:80' proxy=urllib2.ProxyHandler({"https":'https://'+proxy}) opener = urllib2.build_opener(proxy) request = urllib2.Request('https://somehttpspage....') response = opener.open(request)
0
7908
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8336
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7950
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6606
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
2343
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.