Esempio n. 1
0
    def request(self, url):
        #Create default ssl context to allow HTTPS connections
        context = ssl.create_default_context()

        #Clear data from previous request
        self.data = ''

        #Parse url and connect to host
        host, path = UrlParser.parse_url(url)

        #Connect to host and return if host does not exist
        try:
            sock = socket.create_connection((host, self.port))
            ssock = context.wrap_socket(sock, server_hostname=host)
        except socket.gaierror:
            print('there was an error resolving the host {}'.format(host))
            return
        else:
            with ssock:
                self.sock = ssock
                self.sock.settimeout(2)
                #Send HTTP request
                req = 'GET {} HTTP/1.1\r\nHost: {}\r\n\r\n'.format(path, host)
                print('HTTP request\n{}'.format(req))
                self.sock.send(req.encode())
                print('sent HTTP request')

                #Receive and store response
                buffer = ''
                data = self.sock.recv(1024)
                while data:
                    try:
                        print(data.decode('utf-8'), flush=True)
                        buffer += data.decode('utf-8')
                    except UnicodeError:
                        print("Could not decode a block of data using utf-8")
                    try:
                        data = self.sock.recv(1024)
                    except:
                        data = None
                self.data = buffer
                self.sock.shutdown(socket.SHUT_RDWR)
                print("\n\ndone")
Esempio n. 2
0
def main():
    #Check if file with urls is included
    if len(sys.argv) < 2:
        print("usage: tests.py <file with urls>")
        sys.exit()
    urls = sys.argv[1]
    #Read urls line by line and run tests. The results of each test will be written to its own file
    with open(urls, 'r') as fo:
        count = 0
        for line in fo:
            filename, path = UrlParser.parse_url(line)
            filename = filename.split('.')[0]

            filename = filename + str(count) + '.txt'
            test_request_http(line[:-1], filename)
            count += 1
            filename = filename[:-5] + str(count) + '.txt' #works up to single digit count
            test_request_https(line[:-1], filename) #Get rid of newline at end
            #filename = filename + str(count) + '.txt'
            #test_request_https(line[:-1], filename) #Get rid of newline at end
            count += 1
    sys.exit()
Esempio n. 3
0
    def request(self, url, params={}):
        #Create socket
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.settimeout(2)

        #Clear data from previous request
        self.data = ''

        #Parse url and connect to host
        host, path = UrlParser.parse_url(url)
        if not self.connect(self.sock, host):
            return

        #Send HTTP request
        req = 'GET {} HTTP/1.1\r\nHost: {}\r\n\r\n'.format(path, host)
        print('HTTP request\n{}'.format(req))
        self.sock.send(req.encode())
        print('sent HTTP request')

        #Receive and store response
        buffer = ''
        data = self.sock.recv(1024)
        while data:
            try:
                buffer += data.decode('utf-8')
            except UnicodeDecodeError:
                print("Could not decode a block of data using utf-8")
            try:
                data = self.sock.recv(1024)
            except:
                data = None
        self.data = buffer

        #Close the connection now that you have everything
        self.sock.shutdown(socket.SHUT_RDWR)
        self.sock.close()
        print("connection closed")