Skip to content

Instantly share code, notes, and snippets.

@gregneagle
Created April 17, 2015 16:30
Show Gist options
  • Save gregneagle/1816b650df8e3fbeb18f to your computer and use it in GitHub Desktop.
Save gregneagle/1816b650df8e3fbeb18f to your computer and use it in GitHub Desktop.
gurl test
from munkilib import gurl
def run_connection(options):
connection = gurl.Gurl.alloc().initWithOptions_(options)
percent_complete = -1
bytes_received = 0
connection.start()
try:
while not connection.isDone():
if connection.destination_path:
# only print progress info if we are writing to a file
if connection.percentComplete != -1:
if connection.percentComplete != percent_complete:
percent_complete = connection.percentComplete
print 'Percent complete: %s' % percent_complete
elif connection.bytesReceived != bytes_received:
bytes_received = connection.bytesReceived
print 'Bytes received: %s' % bytes_received
except (KeyboardInterrupt, SystemExit):
# safely kill the connection then fall through
connection.cancel()
except Exception: # too general, I know
# Let us out! ... Safely! Unexpectedly quit dialogs are annoying ...
connection.cancel()
# Re-raise the error
raise
if connection.error != None:
print 'Error:', (connection.error.code(),
connection.error.localizedDescription())
if connection.SSLerror:
print 'SSL error:', connection.SSLerror
if connection.response != None:
# print 'Response:', dir(connection.response)
print 'Status:', connection.status
print 'Headers:', connection.headers
if connection.redirection != []:
print 'Redirection:', connection.redirection
def main():
test_list = [
{
'note': 'Untrusted CA',
'url': 'https://tidia.ita.br/',
'file': '/tmp/foo',
},
{
'note': 'Expired cert',
'url': 'https://testssl-expire.disig.sk/',
'file': '/tmp/foo',
},
{
'note': 'Wrong hostname for cert',
'url': 'https://tv.eurosport.com/',
'file': '/tmp/foo',
},
{
'note': 'Revoked cert',
'url': 'https://test-sspev.verisign.com:2443/',
'file': '/tmp/foo',
},
{
'note': 'HTTP to HTTPS redirect',
'url': 'http://github.com/404',
'file': '/tmp/foo',
'follow_redirects': True,
},
{
'note': '301 redirect',
'url': 'http://jigsaw.w3.org/HTTP/300/301.html',
'file': '/tmp/foo',
'follow_redirects': True,
},
{
'note': '302 redirect',
'url': 'http://jigsaw.w3.org/HTTP/300/302.html',
'file': '/tmp/foo',
'follow_redirects': True,
},
{
'note': '307 redirect',
'url': 'http://jigsaw.w3.org/HTTP/300/307.html',
'file': '/tmp/foo',
'follow_redirects': True,
},
{
'note': 'HTTP Basic Authentication with username and password',
'url': 'http://browserspy.dk/password-ok.php',
'file': '/tmp/foo',
'username': 'test',
'password': 'test',
},
{
'note': 'HTTP Basic Authentication with header',
'url': 'http://browserspy.dk/password-ok.php',
'file': '/tmp/foo',
'additional_headers': {'Authorization': 'Basic dGVzdDp0ZXN0'},
},
{
'note': 'HTTP Digest Authentication',
'url': 'http://httpbin.org/digest-auth/auth/user/passwd',
'file': '/tmp/foo',
'username': 'user',
'password': 'passwd',
},
{
'note': 'Downloading a big file',
'url': 'https://dl.google.com/chrome/mac/stable/GGRO/googlechrome.dmg',
'file': '/tmp/foo',
'can_resume': False,
'download_only_if_changed': False,
},
{
'note': 'Downloading a big file only if changed.',
'url': 'https://dl.google.com/chrome/mac/stable/GGRO/googlechrome.dmg',
'file': '/tmp/foo',
'can_resume': True,
'download_only_if_changed': True,
},
{
'note': 'Accessing a non-existent page at a working web server.',
'url': 'http://www.apple.com/this_url_doesnt_exist',
'file': '/tmp/foo',
},
{
'note': 'Accessing a non-existent web server.',
'url': 'http://foo.bar.com/',
'file': '/tmp/foo',
},
{
'note': 'Accessing Apple SUS catalog.',
'url': 'https://swscan.apple.com/content/catalogs/others/index-10.9-mountainlion-lion-snowleopard-leopard.merged-1.sucatalog',
'file': '/tmp/foo',
},
]
for options in test_list:
print "\n***Testing %s (%s)...***" % (options['url'], options.get('note'))
run_connection(options)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment