Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import os
- import re
- class session(object):
- """symbolyzes a persist connection between me and a website
- """
- def __init__(self, url):
- # the url of the site to be connected to
- self.url = url
- # the domain of the site
- self.base_name = url.replace('https://', '').replace('http://', '').replace('www.', '').split('.')[0]
- # creates a session
- self.s = requests.Session()
- def get(self, url, headers={}, data={}):
- """makes a get request using the session
- @@url - the wanted url
- @@headers - the headers that will be sent with the get request
- @@data - the data to be sent with the get request
- @@remind - saves a copy of the returned page in the same dir order requested in the url
- @@return - the response of the get request
- """
- response = self.s.get(url, headers = headers, data = data)
- url = url.replace(self.url, self.base_name)
- self.html_file(url if url != self.base_name else url + '/index.html', response)
- return response
- def post(self, url, headers={}, data={}):
- """makes a post request using the session
- @@url - the url of the page that the request will be posted to
- @@headers - the headers for the post request
- @@data - the data for the post request
- @@return - the response of the post request
- """
- response = self.s.post(url, headers = headers, data = data)
- return response
- @staticmethod
- def html_file(file_name, response):
- """creates a file, which content is the same as the resposne text
- """
- # in case there is a / in the end of the file name, it removes it
- file_name = os.path.normpath(file_name)
- # in case no extension is mentioned .html will be default
- file_name += '.html' if os.path.splitext(file_name)[1] == '' else ''
- # distinguishes the dirs part and the file part
- splitted_file = os.path.split(file_name)
- # checks whethear the path exists, if not creates it
- if not os.path.exists(splitted_file[0]):
- os.makedirs(splitted_file[0])
- # checks whethear an old copy exists, if it is, puts it in the old copies dir
- if os.path.exists(file_name):
- # count will be added to the name of the file in the copy folder
- count = '1'
- copy_folder = splitted_file[0] + '/copy_' + splitted_file[1]
- # checks whethear the copy folder of this file exists, if not creates it
- if not os.path.exists(copy_folder):
- os.makedirs(copy_folder)
- else:
- # finds the copies numbers, and sort them backwards
- 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)
- # sets the number in the end of the new file
- count = '1' if len(copies) == 0 else str(copies[0] + 1)
- # moves the old copy to the copy dir
- os.rename(file_name, copy_folder + '/' + '(' + count + ')' + splitted_file[1])
- # copies the data into the file
- with open(file_name, 'wb') as file:
- file.write(response.text.encode('utf-8'))
- def make_connection_to_hackthissite():
- """creating a session to hackthissite and connects to my user
- @@return - a session to hackthissite
- """
- USERNAME = 'talmz12'
- PASSWORD = 'a1a051cd989a'
- # creates a session to the site
- s = session('https://www.hackthissite.org')
- # logs into the site
- s.post('https://www.hackthissite.org/user/login', data = {'username' : USERNAME, 'password' : PASSWORD }, headers = {'referer' : 'https://www.hackthissite.org' })
- return s
- if __name__ == '__main__':
- s = make_connection_to_hackthissite()
- s.get('https://www.hackthissite.org/missions/playit/stego/0/')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement