subprocess Popen.stdin.write causes AttributeError - python

I'm working on a short, native (do NOT recommend an outside [non-native] module such as pexpect), cross-platform, insecure remote control application for python (Windows will use py2exe and an exe file). I am using start_new_thread for the blocking calls such as readline(). For some reason, however, I get this string of ugliness as my output:
Unhandled exception in thread started by <function read_stream at 0xb6918730>Unhandled exception in thread started by <function send_stream at 0xb69186f0>
Traceback (most recent call last):
Traceback (most recent call last):
File "main.py", line 17, in read_stream
s.send(pipe.stdout.readline())
AttributeError File "main.py", line 14, in send_stream
pipe.stdin.write(s.recv(4096))
AttributeError: 'NoneType' object has no attribute 'stdin'
: 'NoneType' object has no attribute 'stdout'
Here is my program (main.py):
#!/usr/bin/env python
import socket
import subprocess as sp
from thread import start_new_thread
from platform import system
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('10.0.0.201', 49200))
shell = 'powershell.exe' if system() == 'Windows' else '/bin/bash' # is this right?
pipe = sp.Popen(shell, shell=True, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
entered_command=False
def send_stream(): # send what you get from command center
while True:
pipe.stdin.write(s.recv(4096))
def read_stream(): # send back what is returned from shell command
while True:
s.send(pipe.stdout.readline())
start_new_thread(send_stream, ())
start_new_thread(read_stream, ())
Thanks for your help.

It turns out that the problem is that the program was trying to exit after the two start_new_thread calls because it had reached the end, and caused errors while trying to do so. So I replaced:
start_new_thread(send_stream, ())
start_new_thread(read_stream, ())
With:
start_new_thread(send_stream, ())
read_stream()

Related

ResourceWarning: subprocess 47013 is still running Exception ignored in: <_io.FileIO name=5 mode='rb' closefd=True>

I got trouble to face the error, could you help me?.
Please take a look at my code!
code in file tools.py
from action import Action
import typer
from rich.table import Table
from rich.console import Console
from model import Command
from shell import Shell
import os
db=Action("localhost","root","","db_cmd")
sh=Shell()
console=Console()
app=typer.Typer()
#app.command(short_help="Untuk melihat data")
def show():
lists=db.show_data()
# print(lists)
console.print("[bold magenta]command alias[/bold magenta]!","💻️")
# print(lists)
table=Table(show_header=True, header_style="bold blue")
table.add_column("No", style="dim", width=6, justify="center")
table.add_column("command", min_width=20, justify="center")
table.add_column("isi command", min_width=12, justify="center")
table.add_column("deskripsi", min_width=12, justify="center")
if len(lists) > 0 :
for i, list in enumerate(lists, start=1):
table.add_row(str(i), list.cmd, list.isi_cmd, list.deskripsi)
else:
table.add_row("-","-","-","-")
console.print(table)
#app.command(short_help="Untuk menambahkan data")
def add(cmd:str=None, isi_cmd:str=None, deskripsi:str=None):
typer.echo(f"Menambahkan command {cmd}")
shell=sh.shellName()
#app.command(short_help="Untuk menghapus data")
def delete(position):
cmd=db.cmd(int(position)-1)
typer.echo(f"deleting command {cmd}")
db.delete_data(int(position)-1)
show()
if __name__=="__main__":
app()
code in file shell.py
import os
import subprocess
import signal
class Shell:
def add(self, cmd, isi_cmd, shell):
if shell == "zsh":
os.system(f"printf 'alias {cmd}=\"{isi_cmd}\"\n' >> /home/$USER/.zshrc")
elif shell == "bash":
os.system(f"printf 'alias {cmd}=\"{isi_cmd}\"\n' >> /home/$USER/.bashrc")
elif shell == "fish":
os.system(f"printf 'alias {cmd}=\"{isi_cmd}\"\n' >> /home/$USER/.fzf.bash")
def delete(self):
pass
def shellName(self):
result=subprocess.Popen("echo $SHELL", shell=True, stdout=subprocess.PIPE)
arr=str(result.stdout.read()).split("/")
shell=arr[3].replace("\\n'","")
return shell
shell=Shell()
print(shell.shellName())
When I run the file named shell.py and called the method sh.shellName(), the program was running well
$python3 shell.py
zsh
But when I run the file named tools.py, and sh.shellName() method, it show me error like this
$python3 tools.py add --cmd="ls" --isi-cmd="ls -lah" --deskripsi="Untuk melihat isi file beserta ukurannya"
Exception ignored in: <function Popen.__del__ at 0x7fa7c6d36550>
Traceback (most recent call last):
File "/usr/lib/python3.8/subprocess.py", line 946, in __del__
_warn("subprocess %s is still running" % self.pid,
ResourceWarning: subprocess 48516 is still running
Exception ignored in: <_io.FileIO name=4 mode='rb' closefd=True>
Traceback (most recent call last):
File "/home/fajar/Documents/python/tools_alias_cmd/shell.py", line 24, in <module>
print(shell.shellName())
ResourceWarning: unclosed file <_io.BufferedReader name=4>
zsh
Menambahkan command ls
Exception ignored in: <function Popen.__del__ at 0x7fa7c6d36550>
Traceback (most recent call last):
File "/usr/lib/python3.8/subprocess.py", line 946, in __del__
_warn("subprocess %s is still running" % self.pid,
ResourceWarning: subprocess 48517 is still running
Exception ignored in: <_io.FileIO name=5 mode='rb' closefd=True>
Traceback (most recent call last):
File "tools.py", line 38, in add
shell=sh.shellName()
ResourceWarning: unclosed file <_io.BufferedReader name=5>
I'v been searching all the solutions on google, youtube, and forum, but I got nothing, it doesn't work for my problems. Thanks

signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object

I am trying to write some code to perform some packet sniffing with python
using pyshark.I have the following piece of code:
import pyshark
print('Pyshark demo')
capture = pyshark.LiveCapture(interface='enp0s8')
However when I try to run this script I get the following stack trace:
Pyshark demo
Exception ignored in: <bound method BaseEventLoop.__del__ of <_UnixSelectorEventLoop running=False closed=True debug=False>>
Traceback (most recent call last):
File "/usr/lib/python3.5/asyncio/base_events.py", line 431, in __del__
File "/usr/lib/python3.5/asyncio/unix_events.py", line 58, in close
File "/usr/lib/python3.5/asyncio/unix_events.py", line 139, in remove_signal_handler
File "/usr/lib/python3.5/signal.py", line 47, in signal
TypeError: signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object
I have install pyshark.Do you have any idea what is causing this problem?
I run this on an ubuntu server 16.04
It is related to python issues:
https://bugs.python.org/issue26133
https://bugs.python.org/issue23548
https://bugs.python.org/issue28628?#ok_message=file%2045378%20created%0Amsg%20280182%20created%0Aissue%2028628%20created&#template=item
also see:
Trigger catkin build process from within python

gevent socket with UDP connection

I have spent many hours on this problem but I could not figure out what is going wrong.
I have two programs in python. P1 program uses gevent socket modeul to create two types of connection i.e., TCP and UDP. Each connection is created in a separate greenlet. Program1 runs okay.
Then I have another program P2 that uses Popen from gevent subprocess to invoke P1 as an external program like this:
sub = Popen("python p1.py 1", stdout = PIPE, stderr=STDOUT, shell=True)
As the P1 starts, a greenlet with UDP connection crashes with following error:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/gevent/greenlet.py", line 327, in run
result = self.run(self.args, *self.kwargs)
File "gtest_runner.py", line 883, in run
self.sock.sendto(data, master_addr)
File "/usr/local/lib/python2.7/dist-packages/gevent/socket.py", line 475, in sendto
ret_val = sock.sendto(*args)
TypeError: an integer is required
<Greenlet at 0x7f86a0140730: <bound method Beacon.run of <__main_.Beacon instance at 0x7f86a2c4aa28>>> failed with TypeError
I am using Python 2.7 on ubuntu 14.
Anticipated thanks!

Popen subprocess exception

Sorry if this is a simple question and has been answered before, but I couldn't find it anywhere.
I'm trying to listen to UDP packets and if they are certain packets, run different batch scripts. I have this working correctly, but I have found that if the Popen command doesn't find the file it triggers an exception and the script stops running. Ideally, I want this to print a message and then continue listening for other packets and act upon them, just giving us a message for debugging. Here is the code I have used, how could I do this?
if rxdata == "Camera 1":
from subprocess import Popen
try:
p = Popen("Camera 1.bat", cwd=r"C:\xxx")
stdout, stderr = p.communicate()
except FileNotFoundError():
print('Camera 1.bat not found')
elif rxdata == "Camera 2":
from subprocess import Popen
p = Popen("Camera 2.bat", cwd=r"C:\xxx")
stdout, stderr = p.communicate()
In both examples, I receive the following and the script closes.
Traceback (most recent call last):
File "C:\UDP Listener.py", line 42, in <module>
p = Popen("Camera 1.bat", cwd=r"C:\xxx")
File "C:\Python34\lib\subprocess.py", line 858, in __init__
restore_signals, start_new_session)
File "C:\Python34\lib\subprocess.py", line 1111, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Thanks in advance
Matt
You must not use the brackets behind the FileNotFoundError (don't call it, just "name" it). Test (with Python 2):
try:
b = a
except NameError():
print "NameError caught."
Execution:
Traceback (most recent call last):
File "test.py", line 2, in <module>
b = a
NameError: name 'a' is not defined
For instance, OSError is a type, whereas OSError() creates an instance of this type:
>>> type(OSError)
<type 'type'>
>>> type(OSError())
<type 'exceptions.OSError'>
Strangely, after re-installing python on my PC everything is now working correctly. Not sure what went wrong but when I run the code now and an exception is found then the code prints as expected.
Thanks for your help!

getting error not attribute exit on thread object

I am getting the following error
Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run() File "python/file_download.py", line 30, in run
self._downloadFile(host[0], host[1]) File "python/file_download.py", line 57, in _downloadFile
th.exit() AttributeError: 'Thread' object has no attribute 'exit'
from the code below:
th = threading.Thread(
target=self._fileWriteToDisk,
args=(saveTo, u, file_name),
name="fileWrite_Child_of_%s" % self.getName(),
)
th.setDaemon(False)
th.start()
print "Writing to disk using child: %s " % th.name
th.exit()
There is no need to kill the thread, python threads kill themselves when they are completed.
Abruptly killing a thread is bad practice, you could have resourced that are used by the thread that aren't closed properly. A stop flag is a better way to end a thread, see this for an example.
You should be using the following, instead of hat you have:
th.daemon = False
as th.setDaemon(False) is depreciated.

Categories

Resources