Guest User

Untitled

a guest
Oct 19th, 2017
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. import requests
  2. import os
  3. import re
  4.  
  5. class session(object):
  6. """symbolyzes a persist connection between me and a website
  7. """
  8. def __init__(self, url):
  9. # the url of the site to be connected to
  10. self.url = url
  11. # the domain of the site
  12. self.base_name = url.replace('https://', '').replace('http://', '').replace('www.', '').split('.')[0]
  13.  
  14. # creates a session
  15. self.s = requests.Session()
  16.  
  17. def get(self, url, headers={}, data={}):
  18. """makes a get request using the session
  19.  
  20. @@url - the wanted url
  21. @@headers - the headers that will be sent with the get request
  22. @@data - the data to be sent with the get request
  23.  
  24. @@remind - saves a copy of the returned page in the same dir order requested in the url
  25.  
  26. @@return - the response of the get request
  27. """
  28. response = self.s.get(url, headers = headers, data = data)
  29. url = url.replace(self.url, self.base_name)
  30. self.html_file(url if url != self.base_name else url + '/index.html', response)
  31.  
  32. return response
  33.  
  34. def post(self, url, headers={}, data={}):
  35. """makes a post request using the session
  36.  
  37. @@url - the url of the page that the request will be posted to
  38. @@headers - the headers for the post request
  39. @@data - the data for the post request
  40.  
  41. @@return - the response of the post request
  42. """
  43.  
  44. response = self.s.post(url, headers = headers, data = data)
  45.  
  46. return response
  47.  
  48. @staticmethod
  49. def html_file(file_name, response):
  50. """creates a file, which content is the same as the resposne text
  51. """
  52. # in case there is a / in the end of the file name, it removes it
  53. file_name = os.path.normpath(file_name)
  54. # in case no extension is mentioned .html will be default
  55. file_name += '.html' if os.path.splitext(file_name)[1] == '' else ''
  56. # distinguishes the dirs part and the file part
  57. splitted_file = os.path.split(file_name)
  58.  
  59. # checks whethear the path exists, if not creates it
  60. if not os.path.exists(splitted_file[0]):
  61. os.makedirs(splitted_file[0])
  62.  
  63. # checks whethear an old copy exists, if it is, puts it in the old copies dir
  64. if os.path.exists(file_name):
  65. # count will be added to the name of the file in the copy folder
  66. count = '1'
  67.  
  68. copy_folder = splitted_file[0] + '/copy_' + splitted_file[1]
  69. # checks whethear the copy folder of this file exists, if not creates it
  70. if not os.path.exists(copy_folder):
  71. os.makedirs(copy_folder)
  72. else:
  73. # finds the copies numbers, and sort them backwards
  74. copies = sorted([int(re.findall(r'([0-9]+)', file)[0]) for file in os.listdir(copy_folder) if len(re.findall(r'([0-9]+)', file)) > 0], reverse=True)
  75. # sets the number in the end of the new file
  76. count = '1' if len(copies) == 0 else str(copies[0] + 1)
  77.  
  78. # moves the old copy to the copy dir
  79. os.rename(file_name, copy_folder + '/' + '(' + count + ')' + splitted_file[1])
  80.  
  81. # copies the data into the file
  82. with open(file_name, 'wb') as file:
  83. file.write(response.text.encode('utf-8'))
  84.  
  85. def make_connection_to_hackthissite():
  86. """creating a session to hackthissite and connects to my user
  87.  
  88. @@return - a session to hackthissite
  89. """
  90. USERNAME = 'talmz12'
  91. PASSWORD = 'a1a051cd989a'
  92.  
  93. # creates a session to the site
  94. s = session('https://www.hackthissite.org')
  95. # logs into the site
  96. s.post('https://www.hackthissite.org/user/login', data = {'username' : USERNAME, 'password' : PASSWORD }, headers = {'referer' : 'https://www.hackthissite.org' })
  97.  
  98. return s
  99.  
  100. if __name__ == '__main__':
  101. s = make_connection_to_hackthissite()
  102. s.get('https://www.hackthissite.org/missions/playit/stego/0/')
Advertisement
Add Comment
Please, Sign In to add comment