Can't trap paramiko runtime trace-back error

2024-05-22 Thread Vinode Singh Ujlain via Python-list
When running the code below , I get error as enumerated below. Why am I 
not able to trap this paramiko runtime traceback in try-except block ?


Exception (client): Error reading SSH protocol banner
Traceback (most recent call last):
  File 
"/home/uzi/.local/lib/python3.8/site-packages/paramiko/transport.py", 
line 2327, in _check_banner

    buf = self.packetizer.readline(timeout)
  File 
"/home/uzi/.local/lib/python3.8/site-packages/paramiko/packet.py", line 
381, in readline

    buf += self._read_timeout(timeout)
  File 
"/home/uzi/.local/lib/python3.8/site-packages/paramiko/packet.py", line 
626, in _read_timeout

    raise socket.timeout()
socket.timeout

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File 
"/home/uzi/.local/lib/python3.8/site-packages/paramiko/transport.py", 
line 2143, in run

    self._check_banner()
  File 
"/home/uzi/.local/lib/python3.8/site-packages/paramiko/transport.py", 
line 2331, in _check_banner

    raise SSHException(
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner

SSH error: No existing session

importparamiko
importtime
defexecute():
try:
# Define the server and credentials
ip='p.q.r.s'
un, up, po='name', "passwd", 22
bto, sto=60, 60
ssh_client=paramiko.SSHClient()
ssh_client.load_system_host_keys()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip, port=po, username=un, password=up, 
banner_timeout=bto, timeout=sto)

shell=ssh_client.invoke_shell()
defsend_command(command, wait_time=1):
shell.send(command+'\n')
time.sleep(wait_time) # Give the command some time to execute
whilenotshell.recv_ready():
time.sleep(0.1)
returnshell.recv(8192).decode()
output=send_command('date')
print("Output:\n", output)
delssh_client
exceptparamiko.SSHExceptionase:
print(f"SSH error: {e}")
exceptExceptionase:
print(f"Error: {e}")
finally:
# Close the SSH client connection
delssh_client
execute()

--
Warm Regards,
Vinode Singh Ujlain | https://www.linkedin.com/in/ujlain/

--
https://mail.python.org/mailman/listinfo/python-list


Re: Cprod -- (writing this: itertools.product([0, 1], repeat=N )

2024-05-22 Thread HenHanna via Python-list

dn wrote:


On 22/05/24 07:14, HenHanna via Python-list wrote:


How can i write this function Cprod (Cartesian Product) simply?

     (writing this out: itertools.product([0, 1], repeat=N
)

The value can be a list or a Tuple.

     cprod([0, 1], 1) => ((0) (1))

     cprod([0, 1], 2) => ((0,0) (0,1) (1,0) (1,1))



This works:

    def cprod(x, c):
     if c==1: return [[i] for i in x]
     Sub= cprod(x, c-1)
     return [i  for F in x   for i in [[F]+R for R in Sub]]


-- Is there another way to write [F]+R ???

    Other ways to improve it?



https://python.readthedocs.io/en/stable/library/itertools.html#itertools.product
   Regards,  =dn



Thank you...  That code looks elegant...  I'll study it.
--
https://mail.python.org/mailman/listinfo/python-list