www.Linux-Support.com

  • Increase font size
  • Default font size
  • Decrease font size
Home

Testing HTTP / HTTPs Urls with Python

Print
Article Index
1. The Application
2. Sample Output

python-appThis recipe provides a Python 2.x script to test urls by utilizing HTTP and HTTPS.

The following program provides the following features:

  • by default the following url is connected: https://www.linux-support.com/test/test.txt
  • by adding a url as an optional parameter you are able to connect http and https services
  • just trusted https services are supported
  • received contents are printed to stdout

Hint: This script will receive contents from HTTPs services signed by unofficial CAs or self-signed certificates without any problems.

1. The Application

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env python
# Source: http://www.linux-support.com
# Author: Mario Scondo
#
 
import urllib
import sys
 
# fetch data and print to stdout
class Main(object):
    def fetchIt(self, url):
        print "connecting to %s" %(url)
        url2 = urllib.urlopen(url)
        for line in url2:
            print line.rstrip()
 
# fetch url from command line or use default
if len(sys.argv) > 1:
    url = sys.argv[1]
else:
    url = "http://www.linux-support.com/test/test.txt"
    url = "https://www.linux-support.com/test/test.txt"
 
M = Main()
M.fetchIt(url)
 

2. Sample Output

The following listing provides an example how the application ist working.

# run with default configuration (https)
$ python fetch_url.py
connecting to https://www.linux-support.com/test/test.txt
This is a text message provided for testing
purposes by http(s)://www.linux-support.com.
 
Data location:
  http://www.linux-support.com/test/test.txt
  https://www.linux-support.com/test/test.txt
 
# run with optional parameter (http)
$ python fetch_url.py http://www.linux-support.com/test/test.txt
connecting to http://www.linux-support.com/test/test.txt
This is a text message provided for testing
purposes by http(s)://www.linux-support.com.
 
Data location:
  http://www.linux-support.com/test/test.txt
  https://www.linux-support.com/test/test.txt
 

Related articles:

Last Updated on Saturday, 21 August 2010 07:14