Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.84 KB | None | 0 0
  1. import socket
  2. import sys
  3. from enum import Enum
  4. from datetime import datetime, tzinfo
  5.  
  6. #gmtTimeZone = pytz.timezone('GMT')
  7. #aestTimeZone = pytz.timezone('Australia/Brisbane')
  8.  
  9.  
  10. class Fields(Enum):
  11.     REPLY_CODE = "reply_code"
  12.     REPLY_CODE_MEANING = "reply_code_meaning"
  13.     DATE = "date"
  14.     LAST_MODIFIED = "last_modified"
  15.     CONTENT_ENCODING = "content_encoding"
  16.     MOVED_TO = "moved_to"
  17.  
  18. DATETIME_FORMAT = "%a, %d %b %Y %I:%M:%S %Z"
  19.  
  20. def connect_socket(remote_ip):
  21.     client_port = 80
  22.  
  23.     try:
  24.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  25.         s.connect((remote_ip, client_port))
  26.     except Exception as e:
  27.         print(e)
  28.         sys.exit()
  29.  
  30.     return s;
  31.  
  32. def request_from_socket(socket, request):
  33.     try:
  34.         socket.sendall(request)
  35.         reply = socket.recv(4096)
  36.     except Exception as e:
  37.         print(e)
  38.         sys.exit()
  39.  
  40.     return reply;
  41.  
  42.  
  43. def process_reply(reply):
  44.     reply_information = {}
  45.  
  46.     lines = reply.split(r"\r\n")
  47.     print(lines[0])
  48.     protocol, reply_code, reply_code_meaning = lines.pop(0).split(" ", 2)
  49.     reply_information[Fields.REPLY_CODE] = reply_code
  50.     reply_information[Fields.REPLY_CODE_MEANING] = reply_code_meaning
  51.  
  52.     for line in lines:
  53.         print(line);
  54.         if line.startswith("Date"):
  55.             reply_information[Fields.DATE] = get_date_from_line(line)
  56.         elif line.startswith("Last-Modified"):
  57.             reply_information[Fields.LAST_MODIFIED] = get_lastmodified_from_line(line)
  58.         elif line.startswith("Content-Encoding"):
  59.             reply_information[Fields.CONTENT_ENCODING] = get_contenttype_from_line(line)
  60.             printf("HELLO")
  61.         elif line.startswith("Location"):
  62.             reply_information[Fields.MOVED_TO] = get_movedto_from_line(line)
  63.  
  64.     return reply_information
  65.  
  66. def get_date_from_line(line):
  67.     return "datedatedate"
  68. #    dt = datetime.strptime(line[6:], DATETIME_FORMAT)
  69.  
  70.  #   date = gmtTimeZone.localize(currentTime)
  71.   #  date = date.astimezone(aestTimeZone)
  72.  
  73.    # return date.strtime(DATETIME_FORMAT)
  74.    
  75. def get_lastmodified_from_line(line):
  76.     return "lastmodifiedlastmodified"
  77. #    dt = datetime.strptime(line[15:], DATETIME_FORMAT)
  78.  
  79.  #   date = gmtTimeZone.localize(currentTime)
  80.   #  date = date.astimezone(aestTimeZone)
  81.  
  82.    # return date.strtime(DATETIME_FORMAT)
  83.  
  84. def get_contenttype_from_line(line):
  85.     return line.split(": ")[1];
  86.  
  87. def get_movedto_from_line(line):
  88.     return line.split(": ")[1];
  89.  
  90. def main():
  91.     #URL = str(input("Enter URL: "))
  92.     URL = 'abc.net.au'
  93.     get_url_info(URL)
  94.  
  95. def get_url_info(URL):
  96.     request = bytes('HEAD / HTTP/1.1\r\n\Accept-Encoding: gzip, deflate, compress\r\n\r\n','utf-8')
  97.     #request = bytes('HEAD / HTTP/1.1\r\n\r\n','utf-8')
  98.     remote_ip = socket.gethostbyname(URL)
  99.    
  100.     s = connect_socket(remote_ip)
  101.     reply = request_from_socket(s, request)
  102.     print(str(reply) + "\n\n")
  103.    
  104.     #reply_information = process_reply("b'HTTP/1.1 200 OK\r\nServer: nginx/1.8.1\r\nDate: Mon, 12 Mar 2018 04:55:57 GMT\r\nContent-Type:" +
  105.     #                                  " text/html\r\nContent-Length: 612\r\nLast-Modified: Tue, 22 Mar 2016 11:25:26 GMT\r\nContent-Encoding:" +
  106.      #                                 " gzip\r\nConnection: close\r\nVary: Accept-Encoding\r\nCache-Control:" +
  107.       #                                " public\r\nX-Served-By: 10.138.100.79\r\nAccept-Ranges: bytes\r\n\r\n'") #str(reply)+ "\r\nContent-Encoding: gzip\r\n")
  108.     reply_information = process_reply(str(reply))
  109.    
  110.     print("HTTP Protocol Analyzer, Written by Samuel Eadie, 44353607\n")
  111.     print("URL Requested: " + URL + "\n")
  112.     print("IP Address, Port of the Server: " + s.getpeername()[0] + ", " + str(s.getpeername()[1]) + "\n")
  113.     print("IP Address, Port of the Client: " + s.getsockname()[0] + ", " + str(s.getsockname()[1]) + "\n")
  114.     print("Reply Code: " + reply_information.get(Fields.REPLY_CODE) + "\n")
  115.     print("Reply Code Meaning: " + reply_information.get(Fields.REPLY_CODE_MEANING) + "\n")
  116.     print("Date: " + str(reply_information.get(Fields.DATE)) + "\n")
  117.     if Fields.LAST_MODIFIED in reply_information.keys():
  118.         print("Last-Modified: " + reply_information.get(Fields.LAST_MODIFIED) + "\n")
  119.     if Fields.CONTENT_ENCODING in reply_information.keys():
  120.         print("Content-Encoding: " + reply_information.get(Fields.CONTENT_ENCODING) + "\n")
  121.  
  122.     if Fields.MOVED_TO in reply_information.keys():
  123.         print("Moved to: " + reply_information.get(Fields.MOVED_TO) + "\n")
  124.         if(reply_information.get(Fields.MOVED_TO).startswith("http://")):
  125.             print("new URL: " + str(reply_information.get(Fields.MOVED_TO).split("http://")[1]))
  126.             get_url_info(reply_information.get(Fields.MOVED_TO).split("http://")[1])
  127.        
  128.  
  129.    
  130.    
  131.     s.close()
  132.    
  133.  
  134. if __name__ == "__main__":
  135.     main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement