D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
proc
/
2
/
root
/
proc
/
2
/
root
/
opt
/
imh-python
/
lib
/
python2.7
/
site-packages
/
cmu_ded
/
Libs
/
Filename :
checker.py
back
Copy
""" Definitions specifically to check things. By Richard Kostyn. """ import ssl import urllib2 from operator import xor from urllib2 import HTTPError from urllib2 import URLError def check_urls(url1, url2): """ Takes in two URL's and separately goes through and find if the other URL doesn't have it. Depending on the response code, we should be good. Also finding a way around HTTP 501, 404 for wordpress and 500. For the future I want to print in the STR what error code. :param url1: First URL. :param url2: Second URL. :return: An array full of everything missing. The array will be emtpy if there is nothing. """ mismatch = [] # Not checking SSL's to validate. ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE # Sending blank auth to go around any password protected website without urllib asking for a password. # If it does ask for a password it will stall the process. headers = {} req1 = urllib2.Request(url1) req2 = urllib2.Request(url2) content_url1 = None content_url2 = None try: content_url1 = urllib2.urlopen(req1, context=ctx) content_url2 = urllib2.urlopen(req2, context=ctx) except HTTPError as e: if e.code == 401: mismatch.append("Website is behind authentication") pass elif e.code == 403: # If we can't access it then we shouldn't do anything. pass elif e.code == 404: wordpress = False if 'link' in list(e.hdrs) and 'wp-json' in e.hdrs['link']: wordpress = True if not wordpress: mismatch.append("404 in one of the URL's.") elif e.code == 500: print "500 on the URL." mismatch.append("500 in the URL") pass else: print "Issues in opening %s and %s" % (url1, url2) mismatch.append("Issues in opening %s and %s" % (url1, url2)) pass except URLError as e: mismatch.append(e.reason) except Exception as e: mismatch.append("Generic Failure on %s and %s." %(url1, url2)) if content_url1 is not None and content_url2 is not None: # Checking headers for a file being downloaded. url1_contents = content_url1.headers url2_contents = content_url2.headers if (url1_contents.get('content-length') is None) ^ \ (url2_contents.get('content-length') is None): return ["One of the Temp URL's is downloading a file. Please check the .htaccess"] # After checking headers we the content: content_url1 = content_url1.readlines() content_url2 = content_url2.readlines() for url1_line in content_url1: if url1_line not in content_url2: mismatch.append('[-]: ' + url1_line) for url2_line in content_url2: if url2_line not in content_url1: mismatch.append('[+]: ' + url2_line) return mismatch def validate_temp_urls(from_host, to_host, user): """ Generated temp_url's and checks them. :param from_host: The host the customer is coming from. :param to_host: The host the customer is going to. :param user: The user you want to check. :return: The result. Its in a list full of everything different. [+] is something new on the server, while [-] is something missing on the new server. """ result = [] url1 = gen_temp_url(from_host, user) url2 = gen_temp_url(to_host, user) result = check_urls(url1, url2) return result def gen_temp_url(host, user): """ Temp URL stuff. :param host: Host you want to check :param user: User you want to check :return: The full temp url. """ return 'http://' + host + '/~' + user if __name__ == '__main__': # Personal tests. bad_stuff = check_urls('http://vps12736.inmotionhosting.com/~rnk/', 'http://vps30181.inmotionhosting.com/~rnk/') for line in bad_stuff: print line