import urllib2
import urllib
import time
# simple INFO and GET
url = "http://kittyandbear.net"
print "downloading" , url
usock = urllib2.urlopen( url )
print usock.info()
time.sleep(3)
data = usock.read()
usock.close()
print data
# POST
url = "http://network-tools.com"
parameters = dict( prog="express" , host="108.60.123.146" )
encoded_parameters = urllib.urlencode( parameters )
usock = urllib2.urlopen( url , encoded_parameters )
data = usock.read()
usock.close()
print str( data )
@staticmethod
def upload_large_file_copies( upload_url ):
import MultipartPostHandler
import urllib2
import cookielib
for i in xrange( file_count ):
file_name = str( i ) + file_name # from os.base...
cookies = cookielib.CookieJar()
opener = urllib2.build_opener( urllib2.HTTPCookieProcessor(cookies), MultipartPostHandler.MultipartPostHandler )
params = { "file" : open( local_file_path , "rb" ) }
upload_sock = opener.open( upload_url , params )
response = upload_sock.read()
upload_sock.close()
"""
# https connection to github using urllib2 and manually adding the Basic Authorization header
import urllib2
from base64 import b64encode
request = urllib2.Request('https://api.github.com/user')
request.add_header('Authorization', 'Basic ' + b64encode('user' + ':' + 'pass'))
r = urllib2.urlopen(request)
print r.getcode()
print r.headers["content-type"]
print r.headers["X-RateLimit-Limit"]
"""
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# httplib is lower level than urllib2, so you can re-use a connection
conn = httplib.HTTPConnection('kittyandbear.net')
conn.request('GET', '/')
r1 = conn.getresponse()
print r1.status, r1.reason
conn.close()
conn = httplib.HTTPSConnection('john-pfeiffer.appspot.com')
conn.request('GET', '/time')
r1 = conn.getresponse()
print r1.status, r1.reason
data = r1.read()
print data
conn.request('GET', '/time?timezone=PST')
data = r1.read()
print data
conn.close()
import httplib, urllib
params = urllib.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
conn = httplib.HTTPConnection("bugs.python.org")
conn.request("POST", "", params, headers)
response = conn.getresponse()
print response.status, response.reason # 302 Found
data = response.read() # 'Redirecting to <a href="http://bugs.python.org/issue12524">http://bugs.python.org/issue12524</a>'
conn.close()
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
""" use https://github.com/henrik/hipchat-emoticons/blob/master/emoticons.json to get global emoticons
{
"shortcut": ":D",
"width": 18,
"height": 18,
"image": "bigsmile.png"
}
https://dujrsrsgsd3nh.cloudfront.net/img/emoticons/allthethings.png
"""
import time
import urllib2
import json
emoticon_list_host = 'raw.github.com'
emoticon_list_resource = '/henrik/hipchat-emoticons/master/emoticons.json'
emoticon_list_url = 'https://' + emoticon_list_host + emoticon_list_resource
emoticon_source_host = 'dujrsrsgsd3nh.cloudfront.net'
emoticon_source_resource_fragment = '/img/emoticons/'
emoticon_source_url = 'https://' + emoticon_source_host + emoticon_source_resource_fragment
def get_emoticon_listing():
usock = urllib2.urlopen(emoticon_list_url)
data = usock.read()
usock.close()
emoticon_list = json.loads(data)
return emoticon_list
def get_emoticon_filename(emoticon_dictionary):
filename = emoticon_dictionary.get('image')
emoticon_filename_parts = filename.split('/')
return emoticon_filename_parts[-1]
def get_emoticon_source_name(emoticon_dictionary):
return emoticon_dictionary.get('image')
def download_emoticon_file(source_name, filename):
usock = urllib2.urlopen(emoticon_source_url + source_name)
data = usock.read()
with open(filename, 'wb') as f:
f.write(data)
usock.close()
def httplib_get_emoticon_listing():
emoticon_list = list()
connection = httplib.HTTPSConnection(emoticon_list_host)
connection.request('GET', emoticon_list_resource)
response = connection.getresponse()
if response.status == 200:
data = response.read()
emoticon_list = json.loads(data)
else:
print 'ERROR: could not get emoticon list: {}'.format(response.status)
connection.close()
return emoticon_list
def httplib_download_emoticon_file(source_name, filename, conn):
conn.request('GET', emoticon_source_resource_fragment + source_name)
response = conn.getresponse()
if response.status == 200:
data = response.read()
with open(filename, 'wb') as f:
f.write(data)
else:
print 'ERROR: could not download emoticon: {}'.format(response.status)
if __name__ == '__main__':
import httplib
start = time.time()
connection = httplib.HTTPSConnection(emoticon_source_host)
for emoticon in httplib_get_emoticon_listing():
emoticon_source_name = get_emoticon_source_name(emoticon)
emoticon_filename = get_emoticon_filename(emoticon)
print emoticon_source_name, emoticon_filename
httplib_download_emoticon_file(emoticon_source_name, emoticon_filename, connection)
connection.close()
print 'finished: {} seconds\n'.format(time.time() - start)
# do it again and verify re-using connections is faster (5.5 seconds to 2 seconds)
start = time.time()
try:
for emoticon in get_emoticon_listing():
emoticon_source_name = get_emoticon_source_name(emoticon)
emoticon_filename = get_emoticon_filename(emoticon)
print emoticon_source_name, emoticon_filename
download_emoticon_file(emoticon_source_name, emoticon_filename)
except urllib2.HTTPError as error:
print 'HTTPERROR:', error.code
except urllib2.URLError as error:
print 'URLERROR:', error.reason
except IOError as error:
print 'IOERROR:', error
print 'finished: {} seconds'.format(time.time() - start)