python webbrowser module not working on mac 16.2.1 - python-3.x

I'm trying to use the webbrowser module to open a web page.
The first attempt leads to nowhere. code hangs, nothing happens.
After a keyboard interrupt the same command - and everything works just fine.
>>> import webbrowser
>>> webbrowser._browsers
{}
>>> webbrowser.register_standard_browsers()
^CTraceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python#3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/webbrowser.py", line 555, in register_standard_browsers
raw_result = subprocess.check_output(cmd, stderr=subprocess.DEVNULL)
File "/usr/local/Cellar/python#3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/subprocess.py", line 421, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,File "/usr/local/Cellar/python#3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/subprocess.py", line 505, in run
stdout, stderr = process.communicate(input, timeout=timeout)
File "/usr/local/Cellar/python#3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/subprocess.py", line 1141, in communicate
stdout = self.stdout.read()
KeyboardInterrupt
>>> webbrowser._browsers
{'macosx': [None, <webbrowser.MacOSXOSAScript object at 0x10f08c5e0>], 'chrome': [None, <webbrowser.MacOSXOSAScript object at 0x10f08c670>], 'firefox': [None, <webbrowser.MacOSXOSAScript object at 0x10f08c760>], 'safari': [None, <webbrowser.MacOSXOSAScript object at 0x10efb4370>]}
Traceback tells me it stucks somewhere in the register process.
So i tried to register all standard browsers -> same behavior. But after keyboard interrupt webbrowser._browsers returned registered browsers.
Tried to register chrome browser - also the same.
webbrowser._browsers['chrome'][1].open("https://www.google.com/")
also needs an interrupt but that command opens the site directly after keyboard interrupt
Problem is the same from py version 3.9 - 3.11. Also on pycharm within or without a virtual environment.
Can anybody reproduce this or tell me what's going on?

Related

Two errors when trying to open firefox using selenium (Python); System cannot find the file specified; Geckodriver executable needs to be in PATH

import time
from selenium import webdriver
path = input("enter filepath: ")
driver = webdriver.Firefox(path)
driver.get("htpps://google.com")
This is the part of my code seemingly causing the error, I have been able to find any answers online. The code used to work when using r"c:\users\ellio\desktop\main" however this has since stopped working too, only without the top error. I have added the folder containg geckodriver to path and added the geckodriver exe to the same file as my code, but nothing seems to work.
Thanks in advance for any help.
Here are the errors
Traceback (most recent call last):
File "C:\Users\ellio\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "C:\Users\ellio\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\ellio\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\ellio\Desktop\main\app.py", line 18, in <module>
driver = webdriver.Firefox(path)
File "C:\Users\ellio\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 164, in __init__
self.service.start()
File "C:\Users\ellio\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
give a specific path of your driver
from selenium import webdriver
driver = webdriver.Chrome('D:\\chrome\\chromedriver.exe') #give location of driver you download
driver.get('http://www.google.com/');
time.sleep(5)
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5)
driver.quit()

Failed to create a device file using `uinput` module. Python3 on Ubuntu for Windows

I have a python script that runs on python3.4 and uses the package keyboard to allow for keybinds;
keyboard.add_hotkey("enter", self.listener.stop, suppress=True)
keyboard.add_hotkey("shift+enter", self.listener.finish, suppress=True)
When I run this on Windows, it works perfectly listening to both hotkeys, also when run on linux (CentOS) it works.
At work I've gotten a Ubuntu environment on my windows via the windows 10 feature and app store. However this environment has a problem with this keyboard hotkey.
/usr/local/lib/python3.6/dist-packages/keyboard-0.13.2-py3.6.egg/keyboard/_nixkeyboard.py:110: UserWarning: Failed to create a device file using `uinput` module. Sending of events may be limited or unavailable depending on plugged-in devices.
device = aggregate_devices('kbd')
Traceback (most recent call last):
File "main.py", line 32, in <module>
], 'test')
File "/mnt/.../can_controller.py", line 28, in __init__
self.__initialise_key_handler()
File "/mnt/.../can_controller.py", line 95, in __initialise_key_handler
keyboard.add_hotkey("enter", self.listener.stop, suppress=True)
File "/usr/local/lib/python3.6/dist-packages/keyboard-0.13.2-py3.6.egg/keyboard/__init__.py", line 637, in add_hotkey
_listener.start_if_necessary()
File "/usr/local/lib/python3.6/dist-packages/keyboard-0.13.2-py3.6.egg/keyboard/_generic.py", line 35, in start_if_necessary
self.init()
File "/usr/local/lib/python3.6/dist-packages/keyboard-0.13.2-py3.6.egg/keyboard/__init__.py", line 194, in init
_os_keyboard.init()
File "/usr/local/lib/python3.6/dist-packages/keyboard-0.13.2-py3.6.egg/keyboard/_nixkeyboard.py", line 113, in init
build_device()
File "/usr/local/lib/python3.6/dist-packages/keyboard-0.13.2-py3.6.egg/keyboard/_nixkeyboard.py", line 110, in build_device
device = aggregate_devices('kbd')
File "/usr/local/lib/python3.6/dist-packages/keyboard-0.13.2-py3.6.egg/keyboard/_nixcommon.py", line 168, in aggregate_devices
assert fake_device
AssertionError
If anybody knows how to fix this or has a good work-around. Please let me know.

Paramiko sftp put request getting terminated with EOFError() in python

I'm trying to upload couple of .zip files with size ranging between 20 to 30 MBs using python's sftp module 'Paramiko'. I'm able to successfully connect to the sftp server, and also able to list the contents in the destination directory. But while attempting to upload files from my ec2 linux machine, the request is taking too much time and then terminating with EOFError(). Below is the code,
>>> import paramiko
>>> import os
>>> ftp_host='*****'
>>> ftp_username='***'
>>> ftp_password='**'
>>> ssh_client=paramiko.SSHClient()
>>> ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh_client.connect(hostname=ftp_host,username=ftp_username,password=ftp_password, timeout=500)
>>> ftp_client=ssh_client.open_sftp()
>>> ftp_client.chdir('/inbound/')
>>> ftp_client.listdir()
[u't5-file-1.zip', u'edp_r.zip', u'T5_Transaction_Sample.gz']
>>> ftp_client.put("/path-to-zip-files-on-local/edp_revenue.zip","/inbound/edp_revenue.zip")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 669, in put
return self.putfo(fl, remotepath, file_size, callback, confirm)
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 626, in putfo
fr.write(data)
File "/usr/lib/python2.7/dist-packages/paramiko/file.py", line 331, in write
self._write_all(data)
File "/usr/lib/python2.7/dist-packages/paramiko/file.py", line 448, in _write_all
count = self._write(data)
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_file.py", line 176, in _write
self._reqs.append(self.sftp._async_request(type(None), CMD_WRITE, self.handle, long(self._realpos), data[:chunk]))
File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 750, in _async_request
self._send_packet(t, msg)
File "/usr/lib/python2.7/dist-packages/paramiko/sftp.py", line 170, in _send_packet
self._write_all(out)
File "/usr/lib/python2.7/dist-packages/paramiko/sftp.py", line 135, in _write_all
raise EOFError()
EOFError
When I'm trying the same thing using curl command I'm easily able to do and just in 20 seconds. What am I missing here? Is it the size which is making the request to timed out or anything else.
It seems I was using an older version of paramiko on my linux server. I upgraded it using,
pip install --upgraded paramiko
and then logged everything in a log file,
paramiko.util.log_to_file('/path_to_log_file/paramiko.log')
just to capture every detail. Now it seems to be working pretty fast and correct.

pexpect telnet on windows

I've used pexpect on linux successfully to telnet/ssh into Cisco switches for CLI scrapping. I'm trying to convert this code over to Windows and having some issues since it doesn't support the pxpect.spawn() command.
I read some online documentation and it suggested to use the pexpect.popen_spawn.PopenSpawn command. Can someone please point to me what I'm doing wrong here? I removed all my exception handling to simplify the code. Thanks.
import pexpect
from pexpect.popen_spawn import PopenSpawn
child = pexpect.popen_spawn.PopenSpawn('C:/Windows/System32/telnet 192.168.1.1')
child.expect('Username:')
child.sendline('cisco')
child.expect('Password:')
child.sendline('cisco')
child.expect('>')
child.close()
Error:
Traceback (most recent call last):
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\expect.py", line 98, in expect_loop
incoming = spawn.read_nonblocking(spawn.maxread, timeout)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\popen_spawn.py", line 68, in read_nonblocking
raise EOF('End Of File (EOF).')
pexpect.exceptions.EOF: End Of File (EOF).
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python\windows scripts\telnet\telnet.py", line 37, in <module>
main()
File "C:\Python\windows scripts\telnet\telnet.py", line 20, in main
child.expect('Username:')
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\spawnbase.py", line 327, in expect
timeout, searchwindowsize, async_)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\spawnbase.py", line 355, in expect_list
return exp.expect_loop(timeout)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\expect.py", line 104, in expect_loop
return self.eof(e)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\expect.py", line 50, in eof
raise EOF(msg)
pexpect.exceptions.EOF: End Of File (EOF).
<pexpect.popen_spawn.PopenSpawn object at 0x000000000328C550>
searcher: searcher_re:
0: re.compile("b'Username:'")
The solution is to use plink.exe, which is a part of putty installation. You can download it from https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
Enable TelnetClient on windows system and put plink.exe in the same folder from where you are running pexpect. Then you can telnet using pexpect as mentioned in the below example.
Also, use timeout flag with PopenSpawn to wait for the connection to establish. The above error is due to a timeout flag is not set.
p = pexpect.popen_spawn.PopenSpawn('plink.exe -telnet 192.168.0.1 -P 23', timeout=1)

Switching to new Tor identity on Android using QPython

After successfully installing QPython and Orbot on my Android device, I've pip-installed a well-known stem library and tried to use it for changing Tor identity by sending NEWNYM signal to control port.
On PC, this code
from stem.control import Controller
from stem import Signal
with Controller.from_port(port=9051) as ctrl:
ctrl.authenticate()
ctrl.signal(Signal.NEWNYM)
works perfectly.
But, on Android device, QPython returns an error:TypeError: decode() argument 1 must be string, not None. Authentication fails.
How could I overcome this problem? Maybe, I should look towards AnroidFacade SL4A API to interact with Tor?
I'am running Android 7.1 at Nokia 3. ControlPort was set to constantly be 9051 by using Torrc Custom Config in Orbot Settings.
Upd Here's a traceback stack:
/data/user/0/org.qpython.qpy/files/bin/qpython-android5.sh "/storage/emulated/0/qpython/scripts/new.py" && exit
d/0/qpython/scripts/new.py" && exit <
Traceback (most recent call last):
File "/storage/emulated/0/qpython/scripts/new.py", line 6, in <module>
ctrl.authenticate()
File "/data/user/0/org.qpython.qpy/files/lib/python2.7/site-packages/stem-1.6.0-py2.7.egg/stem/control.py", line 1092, in authenticate
stem.connection.authenticate(self, *args, **kwargs)
File "/data/user/0/org.qpython.qpy/files/lib/python2.7/site-packages/stem-1.6.0-py2.7.egg/stem/connection.py", line 530, in authenticate
protocolinfo_response = get_protocolinfo(controller)
File "/data/user/0/org.qpython.qpy/files/lib/python2.7/site-packages/stem-1.6.0-py2.7.egg/stem/connection.py", line 1022, in get_protocolinfo
stem.response.convert('PROTOCOLINFO', protocolinfo_response)
File "/data/user/0/org.qpython.qpy/files/lib/python2.7/site-packages/stem-1.6.0-py2.7.egg/stem/response/__init__.py", line 124, in convert
message._parse_message(**kwargs)
File "/data/user/0/org.qpython.qpy/files/lib/python2.7/site-packages/stem-1.6.0-py2.7.egg/stem/response/protocolinfo.py", line 110, in _parse_message
self.cookie_path = line.pop_mapping(True, True, get_bytes = True)[1].decode(sys.getfilesystemencoding())
TypeError: decode() argument 1 must be string, not None
1|NE1:/ $

Resources