0

Hi I want to capture the PCAP files for some website. I want to start capturing the packets before I get the url using selenium. My challenge is I need to add timestamp to it initial_time and end_time. the output need to be like this::

<url> <start_time>
<timestamp>: <direction_of_packet>:<size_of_packet>....<end_time>

the packet capture need to start before time. Now the problem is I do not know how to manage this time because the arguments

from selenium.common.exceptions import TimeoutException
from selenium import webdriver
import time
import os
import pyshark


class Test():

    def __init__(self):
        # TASK 2 C timeout from CLI
        self.timeout = int(input('Enter the timeout:: '))
        # TASK 2 J number of trace from CLI
        self.network_trace = int(input('Enter number of trace:: '))


    def start_time(self):
        self.initial_time = time.time()
    

    def end_time(self):
            # end time stamp     
        self.finish_time = time.time()
    


    def packet_trace(self, initial_time, finish_time, url):
        capture = pyshark.LiveCapture(interface='enp0s25')
        # TASK 2 D
        for packet in capture.sniff_continuously(packet_count=self.network_trace):
            path = '/home/Desktop/Task_2/trace_details/'
            packet_details_path = os.path.join(path, 'files.pcap')
            with codecs.open(packet_details_path, 'w+' , 'utf-8') as write_data:
                write_data.write(f'start_time:: {initial_time}\n f'url:: {url}' \n packet_details:: {packet} \n end_time:: {finish_time}\n -----------------------------------------------')



    def connection_firefox(self, initial_time, finish_time):
        url = ['google.com','apple.com']
        for url in self.url_list:
            try:
                full_url = 'https://' + url
                domain = url.split('.')[0]
                driver = webdriver.Firefox()
            
                # print(start_time)
                try:
                    # avoiding unnecessary time TASK 2 C max timeout comming from __init__
                    driver.set_page_load_timeout(self.timeout)


                    self.packet_trace(initial_time,full_url, finish_time)

                    self.start_time()

                    driver.get(full_url)

                    self.end_time()

                    # TASK F loading timeout
                except TimeoutException as e:
                    path = '/homeDesktop/Task_2/errors'
                    write_error = os.path.join(path, 'timeout_in_' + domain + '.txt')
                    write_mode = codecs.open(write_error, 'w+' , 'utf-8')
                    write_mode.write(f'url:: {full_url} \n time out error:: {e} \n -----------------------------------------------')
                    driver.close()

                #print(end_time)
            
                # total time taken
                total_time = self.initial_time - self.finish_time
                print(f'total time {total_time}')
                #print(total_time)

                # TASK 2 H html code
            
                html = domain +'.html'
                html_source_location = os.path.join('/home/Desktop/Task_2/html_codes', html)

                # open file in write mode
                write_mode = codecs.open(html_source_location, 'w+' , 'utf-8')
                # obtain html code
                html_source = driver.page_source
                write_mode.write(html_source)
                # TASK 2 G taking screen shot  
                driver.save_screenshot(f"/home/Desktop/Task_2/images/{domain}.png")
                # TASK 2 G END

            
                driver.close()
                
                    # TASK 2 i error collecting
            except Error as e:
                path = '/home/Desktop/Task_2/errors'
                write_error = os.path.join(path, domain + '.txt')
                write_mode = codecs.open(write_error, 'w+' , 'utf-8')
                write_mode.write.write(f'url:: {full_url} \n error:: {e} \n -----------------------------------------------')
                driver.close()

    
        
Browser = Test()
Browser.connection_firefox()
wawo lala
  • 1
  • 2

0 Answers0