def __init__(self):
display = Display(visible=0, size=(1600, 1024))
display.start()
self.driver = webdriver.Firefox()
self.driver.delete_all_cookies()
python类Display()的实例源码
def __init__(self, username=None, password=None, nogui=False):
if nogui:
self.display = Display(visible=0, size=(800, 600))
self.display.start()
chrome_options = Options()
chrome_options.add_argument('--dns-prefetch-disable')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--lang=en-US')
chrome_options.add_experimental_option('prefs', {'intl.accept_languages': 'en-US'})
# managed_default_content_settings.images = 2: Disable images load, this setting can improve pageload & save bandwidth
# default_content_setting_values.notifications = 2: Disable notifications
# credentials_enable_service & password_manager_enabled = false: Ignore save password prompt from chrome
chrome_prefs = {
'intl.accept_languages': 'en-US',
'profile.managed_default_content_settings.images': 2,
'profile.default_content_setting_values.notifications': 2,
'credentials_enable_service': False,
'profile': {
'password_manager_enabled': False
}
}
chrome_options.add_experimental_option('prefs', chrome_prefs)
self.browser = webdriver.Chrome('./assets/chromedriver', chrome_options=chrome_options)
self.browser.implicitly_wait(5)
self.logFile = open('./logs/logFile.txt', 'a')
self.logFile.write('Session started - %s\n' \
% (datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
if not username or not password:
print('Please provide Username and Password')
return
self.username = username
self.password = password
self.nogui = nogui
self.followed = 0
self.ignore_users = []
self.aborting = False
def get_email_thread_content(ticket, emails):
"""
Generate `abuse.models.Ticket` emails thred history
based on 'email_thread' `abuse.models.MailTemplate`
:param `abuse.models.Ticket` ticket: The cererus ticket
:param list emails: a list of `adapters.services.mailer.abstract.Email`
:rtype: tuple
:return: The content and the filetype
"""
try:
template = MailTemplate.objects.get(codename='email_thread')
is_html = '<html>' in template.body
except ObjectDoesNotExist:
raise EmailThreadTemplateNotFound('Unable to find email thread template')
_emails = []
for email in emails:
_emails.append(Email(
sender=email.sender,
subject=email.subject,
recipient=email.recipient,
body=email.body.replace('\n', '<br>') if is_html else email.body,
created=datetime.fromtimestamp(email.created),
category=None,
attachments=None,
))
domain = ticket.service.name if ticket.service else None
try:
template = loader.get_template_from_string(template.body)
context = Context({
'publicId': ticket.publicId,
'creationDate': ticket.creationDate,
'domain': domain,
'emails': _emails
})
content = template.render(context)
except (TemplateEncodingError, TemplateSyntaxError) as ex:
raise EmailThreadTemplateSyntaxError(str(ex))
try:
import pdfkit
from pyvirtualdisplay import Display
display = Display(visible=0, size=(1366, 768))
display.start()
content = pdfkit.from_string(content, False)
display.stop()
return content, 'application/pdf'
except:
return content.encode('utf-8'), 'text/html' if is_html else 'text/plain'
def entry(arguments):
"""
The entry point for the dreammarket sink CLI interface. This defines the
logic around the usage of command line arguments and the dreammarket sink in
order to perform scraping, ingestion, and storage related functions.
"""
logger.setLevel(arguments.verbosity.upper())
if not arguments.dreammarket_username:
logger.error("This sink requires a username to be specified through CLI or enviornment variable.")
raise SystemExit()
if not arguments.dreammarket_password:
logger.error("This sink requires a password to be specified through CLI or environment variable.")
raise SystemExit()
if not arguments.dbc_access_key:
logger.error("This sink requires a deathbycaptcha access key to be specified through CLI or environment variable.")
raise SystemExit()
if not arguments.dbc_secret_key:
logger.error("This sink requires a deathbycaptcha secret key to be specified through CLI or environment variable.")
raise SystemExit()
display = Display(visible=0, size=(1366, 768))
display.start()
sink = DreammarketSink(
arguments.dreammarket_username, arguments.dreammarket_password,
arguments.dbc_access_key, arguments.dbc_secret_key,
url_file=arguments.url_file,
save_to_directory=arguments.save_to_directory,
onion_url=arguments.onion_url,
request_interval=arguments.request_interval,
request_retries=arguments.request_retries,
request_timeout=arguments.request_timeout,
category=arguments.category
)
sink.logger = logger
if arguments.ingest:
if arguments.datastore == "stdout":
store = STDOutInterface()
parser = DreammarketParser(datastore=store)
parser.parse(scrape_results=sink.scrape())
elif arguments.datastore == "elasticsearch":
store = ElasticsearchInterface(
host=arguments.datastore_host,
port=arguments.datastore_port
)
parser = DreammarketParser(datastore=store)
parser.parse(
scrape_results=sink.scrape(
daemon=arguments.daemonize
)
)
else:
list(sink.scrape())
display.stop()
def firefox_driver(executable_path=None, run_headless=False,
load_images=True, use_proxy=None):
'''Function to initialize ``selenium.webdriver.Firefox`` with extended options
Args:
executable_path (str): path to the ``geckdriver`` binary. If set to ``None`` selenium will search for ``geckdriver`` in ``$PATH``.
run_headless (bool): boolean flag that indicates if ``geckodriver`` has to be headless (without GUI). ``geckodriver`` doesn't support native headless mode, that's why ``pyvirtualdisplay`` is used.
load_images (bool): boolean flag that indicates if Firefox has to render images.
use_proxy (str): use http proxy in <host:port> format.
Returns:
selenium.webdriver.Firefox: created driver.
Note:
In order to create Firefox driver Selenium requires `Firefox <https://www.mozilla.org/en-US/firefox/new/>`_ to be installed and `geckodriver <https://github.com/mozilla/geckodriver/releases>`_ to be downloaded.
Note:
Firefox doesn't support native headless mode. We use ``pyvirtualdisplay`` to simulate it. In order ``pyvirtualdisplay`` to work you need to install ``Xvfb`` package: ``sudo apt install xvfb``.
'''
firefox_profile = webdriver.FirefoxProfile()
if run_headless:
display = Display(visible=0, size=(1024, 768))
display.start()
else:
display = None
if not load_images:
firefox_profile.add_extension(os.path.dirname(
os.path.realpath(__file__)) +
'/browser_extensions/firefox/quickjava-2.1.2-fx.xpi')
# Prevents loading the 'thank you for installing screen'
firefox_profile.set_preference(
'thatoneguydotnet.QuickJava.curVersion', '2.1.2.1')
# Turns images off
firefox_profile.set_preference(
'thatoneguydotnet.QuickJava.startupStatus.Images', 2)
# Turns animated images off
firefox_profile.set_preference(
'thatoneguydotnet.QuickJava.startupStatus.AnimatedImage', 2)
if use_proxy:
_ = use_proxy.split(':')
firefox_profile.set_preference('network.proxy.type', 1)
firefox_profile.set_preference('network.proxy.http', _[0])
firefox_profile.set_preference('network.proxy.http_port', int(_[1]))
firefox_profile.set_preference('network.proxy.ssl', _[0])
firefox_profile.set_preference('network.proxy.ssl_port', int(_[1]))
if executable_path:
driver = webdriver.Firefox(
firefox_profile, executable_path=executable_path)
else:
driver = webdriver.Firefox(firefox_profile)
driver.display = display
return driver
def __init__(self):
config = configparser.ConfigParser()
config.read(os.getcwd() + '/' + 'config.ini')
try:
db = mysql.connector.connect(
host=config['db']['host'],
user=config['db']['user'],
password=config['db']['pass'],
database=config['db']['database']
)
db.autocommit = True
except mysql.connector.errors.ProgrammingError as e:
print " ERROR: Can`t connect to MySQL server! ({0})".format(str(e))
exit(0)
try:
mc = MongoClient(host=config['mongo']['host'], port=int(config['mongo']['port']))
mongo_collection = getattr(mc, config['mongo']['collection'])
except pymongo.errors.ConnectionFailure as e:
print " ERROR: Can`t connect to MongoDB server! ({0})".format(str(e))
exit(0)
R = Registry()
R.set('config', config)
R.set('db', db)
R.set('mongo', mongo_collection)
R.set('wr_path', os.getcwd())
R.set('data_path', os.getcwd() + '/data/')
R.set('http', Http())
R.set('ua', self.random_ua())
R.set('proxies', Proxies())
R.set(
'ndb',
Database(config['db']['host'], config['db']['user'], config['db']['pass'], config['db']['database'])
)
R.set(
'fuzzer_evil_value',
file_get_contents(Registry().get('wr_path') + "/bases/fuzzer-evil-value.txt").strip()
)
R.set('proxy_many_died', False)
R.set('positive_limit_stop', False)
if " ".join(sys.argv).count('selenium') and int(config['selenium']['virtual_display']):
display = Display(visible=0, size=(800, 600))
display.start()
R.set('display', display)