I'm new to Python programming and am attempting to make a script to open URLs/IP addresses with a web browser using Python's webbrowser module.
The actual action of opening the web browser is working fine, but I am getting logging messages from firefox spamming the terminal window that my script is running from. While it doesn't ultimately keep the script from doing its job, it is making it difficult to display prompts as the errors shove them out of view.
These errors seem to be caused when I exit out of the spawned firefox window. These are examples of the errors being displayed:
###!!! [Child][MessageChannel] Error: (msgtype=0x460102,name=PContent::Msg_AsyncMessage) Channel closing: too late to send/recv, messages will be lost
###!!! [Child][MessageChannel] Error: (msgtype=0x2C0048,name=PBrowser::Msg___delete__) Channel closing: too late to send/recv, messages will be lost
I tried to mitigate this by spawning an instance of firefox with a command to send error messages to null instead of the terminal, but this did not help:
os.system("firefox &> /dev/null"); time.sleep(1)
#before calling below in a while loop. opens tabs in the same window that opens from the above command.
webbrowser.open("http://" + urls.pop(0) + ":" + port,new=2)
Any ideas?
Thanks!
My entire script:
"""This script will open URLs and IP addresses in the default web browser.
Please store all URLs and/or IPs in a file called 'port80.txt'.
Please ignore errors that appear in the terminal upon closing your web browser
(may only affect Firefox on Kali Linux).
Thanks."""
import webbrowser
import time
import sys
import os
#sys.tracebacklimit = 0
counter = 0;port = "0"
while port != "80" and port != "443":
port = raw_input("Port 80 or Port 443?\n")
try:
fileOfURLs = open("port"+port+".txt","r")
except:
print("Could not open file. Check if it exists. Exiting...")
exit()
urls = []
readFile = fileOfURLs.readlines()
for line in readFile:
urls.append(line.strip())
fileOfURLs.close()
#get how many urls/IPs are in the file
amountOfURLs = len(urls)
print("There are " + str(amountOfURLs) + " URLs in this text file.")
print("Please ignore error messages upon exit of a FireFox tab/window.\
I have no idea how to stop these...")
"""Open the tabs. Opens 10 at a time over a 10 second span. Change 'maxtabs' to a
different number if more tabs at a time is preferred."""
maxtabs = 10
while amountOfURLs > 0:
os.system("firefox &> /dev/null"); time.sleep(1)
if amountOfURLs > maxtabs:
counter = maxtabs;amountOfURLs -= maxtabs
elif amountOfURLs > 0 and amountOfURLs < maxtabs:
counter = amountOfURLs; amountOfURLs = 0
while counter > 0:
webbrowser.open("http://" + urls.pop(0) + ":" + port,new=2)
counter-=1
print("There are " + str(counter) + " tabs left to open in this batch.")
time.sleep(1)
raw_input("Press enter to continue...")
print("Done.")
raw_input("Press enter to exit...")