Skip to content

Instantly share code, notes, and snippets.

@lsloan
Created June 7, 2019 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsloan/5224b032476aafa7dbe4860a30aa9f47 to your computer and use it in GitHub Desktop.
Save lsloan/5224b032476aafa7dbe4860a30aa9f47 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2 --
# A program I wrote for Univ. of Michigan sysadmins who are
# supporting a legacy system. They need to query various
# URLs, and get the response code and content in return.
# However, they only have an old version of curl available
# to them, one without the option to show content even when
# responses come back with an error-like HTTP status code.
# For example, a 500 code for server error may be returned
# along with content that explains the error. The old version
# of curl ignores the content. This simple Python program
# supports the curl options the sysadmins had been using
# (no certificate validation and a timeout value) and returns
# the HTTP status code and the document content.
import urllib2
import ssl
import sys
(_, url, timeout) = sys.argv
# create context to NOT validate server certificate
certificateContext = ssl.create_default_context()
certificateContext.check_hostname = False
certificateContext.verify_mode = ssl.CERT_NONE
request = urllib2.Request(url)
try:
response = urllib2.urlopen(
request, context=certificateContext, timeout=float(timeout))
responseCode = response.getcode()
content = response.read()
except Exception as error:
responseCode = -1
try:
content = ' | '.join(error)
except:
content = error
print(responseCode)
print(content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment