Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import sys
- from enum import Enum
- from datetime import datetime, tzinfo
- #gmtTimeZone = pytz.timezone('GMT')
- #aestTimeZone = pytz.timezone('Australia/Brisbane')
- class Fields(Enum):
- REPLY_CODE = "reply_code"
- REPLY_CODE_MEANING = "reply_code_meaning"
- DATE = "date"
- LAST_MODIFIED = "last_modified"
- CONTENT_ENCODING = "content_encoding"
- MOVED_TO = "moved_to"
- DATETIME_FORMAT = "%a, %d %b %Y %I:%M:%S %Z"
- def connect_socket(remote_ip):
- client_port = 80
- try:
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.connect((remote_ip, client_port))
- except Exception as e:
- print(e)
- sys.exit()
- return s;
- def request_from_socket(socket, request):
- try:
- socket.sendall(request)
- reply = socket.recv(4096)
- except Exception as e:
- print(e)
- sys.exit()
- return reply;
- def process_reply(reply):
- reply_information = {}
- lines = reply.split(r"\r\n")
- print(lines[0])
- protocol, reply_code, reply_code_meaning = lines.pop(0).split(" ", 2)
- reply_information[Fields.REPLY_CODE] = reply_code
- reply_information[Fields.REPLY_CODE_MEANING] = reply_code_meaning
- for line in lines:
- print(line);
- if line.startswith("Date"):
- reply_information[Fields.DATE] = get_date_from_line(line)
- elif line.startswith("Last-Modified"):
- reply_information[Fields.LAST_MODIFIED] = get_lastmodified_from_line(line)
- elif line.startswith("Content-Encoding"):
- reply_information[Fields.CONTENT_ENCODING] = get_contenttype_from_line(line)
- printf("HELLO")
- elif line.startswith("Location"):
- reply_information[Fields.MOVED_TO] = get_movedto_from_line(line)
- return reply_information
- def get_date_from_line(line):
- return "datedatedate"
- # dt = datetime.strptime(line[6:], DATETIME_FORMAT)
- # date = gmtTimeZone.localize(currentTime)
- # date = date.astimezone(aestTimeZone)
- # return date.strtime(DATETIME_FORMAT)
- def get_lastmodified_from_line(line):
- return "lastmodifiedlastmodified"
- # dt = datetime.strptime(line[15:], DATETIME_FORMAT)
- # date = gmtTimeZone.localize(currentTime)
- # date = date.astimezone(aestTimeZone)
- # return date.strtime(DATETIME_FORMAT)
- def get_contenttype_from_line(line):
- return line.split(": ")[1];
- def get_movedto_from_line(line):
- return line.split(": ")[1];
- def main():
- #URL = str(input("Enter URL: "))
- URL = 'abc.net.au'
- get_url_info(URL)
- def get_url_info(URL):
- request = bytes('HEAD / HTTP/1.1\r\n\Accept-Encoding: gzip, deflate, compress\r\n\r\n','utf-8')
- #request = bytes('HEAD / HTTP/1.1\r\n\r\n','utf-8')
- remote_ip = socket.gethostbyname(URL)
- s = connect_socket(remote_ip)
- reply = request_from_socket(s, request)
- print(str(reply) + "\n\n")
- #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:" +
- # " text/html\r\nContent-Length: 612\r\nLast-Modified: Tue, 22 Mar 2016 11:25:26 GMT\r\nContent-Encoding:" +
- # " gzip\r\nConnection: close\r\nVary: Accept-Encoding\r\nCache-Control:" +
- # " 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")
- reply_information = process_reply(str(reply))
- print("HTTP Protocol Analyzer, Written by Samuel Eadie, 44353607\n")
- print("URL Requested: " + URL + "\n")
- print("IP Address, Port of the Server: " + s.getpeername()[0] + ", " + str(s.getpeername()[1]) + "\n")
- print("IP Address, Port of the Client: " + s.getsockname()[0] + ", " + str(s.getsockname()[1]) + "\n")
- print("Reply Code: " + reply_information.get(Fields.REPLY_CODE) + "\n")
- print("Reply Code Meaning: " + reply_information.get(Fields.REPLY_CODE_MEANING) + "\n")
- print("Date: " + str(reply_information.get(Fields.DATE)) + "\n")
- if Fields.LAST_MODIFIED in reply_information.keys():
- print("Last-Modified: " + reply_information.get(Fields.LAST_MODIFIED) + "\n")
- if Fields.CONTENT_ENCODING in reply_information.keys():
- print("Content-Encoding: " + reply_information.get(Fields.CONTENT_ENCODING) + "\n")
- if Fields.MOVED_TO in reply_information.keys():
- print("Moved to: " + reply_information.get(Fields.MOVED_TO) + "\n")
- if(reply_information.get(Fields.MOVED_TO).startswith("http://")):
- print("new URL: " + str(reply_information.get(Fields.MOVED_TO).split("http://")[1]))
- get_url_info(reply_information.get(Fields.MOVED_TO).split("http://")[1])
- s.close()
- if __name__ == "__main__":
- main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement