Search code examples
pythonhttphttpsurllib2urlopen

How to know if I used http while https was available - Python


I am trying to write a script that checks if HTTPS is available when I used http.
My Idea was to collect all of the HTTP links and use urllib2 in order to open a connection to the server using HTTPS as follows (please ignore syntax problems if there are. I have tried to simplify the code so it will be easier to understand the problem itself):

count=0
for packet in trafficPackets:
   if packet["http.host"] != None:
       if https_supported(packet["ip.dest"]):
           count+=1

where https_supported is the following function:

def https_supported(ip):
   try:
       if len(urlopen("https://"+ip))>0
          return True
   except:
       return False
   return False

I have tried to run the code on a little traffic file which contains an HTTP connection to a site that supports https but the result was unexpected- it was always returned zero.
Where did I go wrong ? Does anyone have an idea of how can I do it?
Thank you!


Solution

  • Using the exact same code with the http.host field instead of the IP seems to work.