I am trying to do a script that is hosting my file on my local network, here's my code :
import os
import getpass
os.system('python -m http.server --directory C:/Users/'+getpass.getuser())
But the probleme is that the http console is showing on my Desktop and that's annoying ! so I tried to hide by renaming the file in .pyw but it's not working.
Have you any idea on how to hide this console ? Thank you :D
On Linux you can use Nohup to ignore the HUP signal.
You could add nohup on your code like this:
import os
import getpass
os.system('nohup python -m http.server --directory C:/Users/'+getpass.getuser())
Update
Solution for windows
import os
import subprocess
import getpass
env = os.environ
directory = 'C:/Users/'+getpass.getuser()
proc = subprocess.Popen(['python', '-m', 'http.server', '--directory', directory], env=env)
Assuming you're on Linux (or other unix based OS), you can detach the process from the console after starting the server.
Here is one way to do it with screen command
sudo apt install -Y screen
And then
screen -d -m "python3 script.py"
Where script.py is the snippet you have shared.
Reference for the flags
...
-d -m
Start screen in detached mode. This creates a new session but doesn’t attach to it. This is useful for system startup scripts.
I found a way to do it, with a VBS script:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "main.py" & Chr(34), 0
Set WshShell = Nothing
Just replace main.py with the path of your script.
Related
How to run sudo bash using python script
import subprocess
import os
sudoPassword2 = 'abcd1234'
command2 = 'sudo bash'
p2 = os.system('echo %s|sudo -S %s' % (sudoPassword2, command2))
I'm getting this error:
bash: line 1: abcd1234: command not found
when i tried to this also its giving error
import shlex
import subprocess
command1 = shlex.split('cd /home/backups')
subprocess.call(command1)
error cd no file or dir
tried also this :
import shlex
import subprocess
subprocess.call(["cd","/home","/backups"])
You can use os module
import os
os.system("sudo and the code you want to run")
in example:
import os
os.system("sudo apt-get vlc")
You are receiving this error because your command posts the password to the cli first without being asked for it. So bash will interpret it as a command which, obviously, can not be executed.
Better do os.system('sudo command') and call the script as root or via sudo. This will make sure you have the necessary privileges within the script immediately at run time.
Another reason why you definitely want to refrain from doing what you do is the necessity of having the sudo password for your machine written into the script in plain text. Never do that. It's evil.
If there is no way around you can make sudo execute a command without asking for a password by adding NOPASSWD directives to the /etc/sudoers by using the editor visudo (never use anything different) like so:
user host = (root) NOPASSWD: /sbin/shutdown
user host = (root) NOPASSWD: /sbin/reboot
But if you do make sure you know that this opens the execution of this command to anyone on the system without needing elevated rights. This can be a huge security risk.
I had wrote scirpt in python which execute bash command using system.os("cmd"). I wouldn't like to have output of bash script on same terminal what I have python script output, so I execute bash command via xterm -e. My code is similar to this:
# python
import os
os.system("xterm -e 'ls'")
This code works but after ls end the new terminal disappear. I want to have stay this terminal.
You can let the the window stay until the user presses a key with read:
os.system("xterm -e 'ls; read'")
or you just run a new terminal of xterm which runs until it is closed:
os.system("xterm")
Note 1: The os.system function seems to block the python script until the external process (xterm in this case) has finished. So you can use it in a loop where each bash window has to be closed before a new one is opened.
Note 2: the python documentation suggests to use subprocess.call
The following should work. I tried it on a Mint linux box.
import os
os.system("xterm -hold -e 'ls' &")
It's almoust good, but:
import os
os.system("xterm -hold -e 'my_cmd_1' &")
os.system("xterm -hold -e 'my_cmd_2' &")
my_cmd_2 can not start before my_cmd_end_1
I am writing a test suite for a web application using Selenium.
In the course of which I need to test behaviour of the app in case a certain service is running or not.
I wanted to create a cgi call to a Python script turning that service on and off.
I know that the cgi call is in the context of the webserver (Apache) however thought that issuing sudo calls like so:
import subprocess
import os
command = 'sudo -S launchctl unload /Library/LaunchAgents/com.my.daemon.plist'
pwd = 'pwd123'
test1 = subprocess.Popen( command, shell=True, stdin=subprocess.PIPE)
test1.communicate(input=pwd)
test2 = os.system( 'echo %s|%s' % (pwd,command) )
would do the trick, well they don't I get return code 256.
What can I do to have this call be executed w/o touching the context in which Apache runs?
As for security: this will only run on a test machine.
The user that Apache runs as needs to be in the /etc/sudoers file, or belong to the sudo group, which I guess it usually doesn't. You also need to make it not ask for a password, which is configured in /etc/sudoers
For Ubuntu, check these out: https://askubuntu.com/questions/7477/how-can-i-add-a-new-user-as-sudoer-using-the-command-line
https://askubuntu.com/questions/147241/execute-sudo-without-password
It could potentially be a pathing issue..
Have you tried writing out the full path like this:
command = '/usr/bin/sudo -S launchctl unload /Library/LaunchAgents/com.my.daemon.plist'
command should be a list, not a string. Try with:
command = ['sudo', '-S', 'launchctl', 'unload', '/Library/LaunchAgents/com.my.daemon.plist']
Cant run sudo this way -- sudo needs a controlling terminal to run.
I am trying to write a python script to automatically scan a section of plex using the Plex Media Scanner. To do so, I must run the scanner as the user running plex (in this case it is 'plex') as well as provide it with the environment variable 'LD_LIBRARY_PATH'. I've tried using both subprocess.call and subprocess.Popen with no difference. In either case, I am not getting any output.
Here is the code I am using:
#!/usr/bin/python
import os
import subprocess
import shlex
env = os.environ.copy()
env['LD_LIBRARY_PATH'] = '/usr/lib/plexmediaserver'
s = "/bin/su - plex -c '/usr/lib/plexmediaserver/Plex\ Media\ Scanner -s -c 2'"
task = shlex.split(s)
exitCode = subprocess.call(task, env=env, shell=True)
Now I already have a working version that does what I want it to do but I had to resort to using a wrapper bash script to do so. You can see the code below:
#!/bin/sh
export LD_LIBRARY_PATH=/usr/lib/plexmediaserver
/usr/lib/plexmediaserver/Plex\ Media\ Scanner $#
And the relevant line of the script which calls it:
exitCode = subprocess.call("/bin/su - plex -c '/var/lib/deluge/delugeScripts/pms.sh -s -c 2'", shell=True)
Thanks for your help.
As jordanm noted in his comment:
the - in su makes it a login shell which re-initializes the environment.
I want to mount a remote directory using sshfs. sshfs working fine from terminal.
But how to invoke it from within python script?
I tried something like this - but didn't work at all.
import os
cmd = "/usr/bin/sshfs giis#giis.co.in:/home/giis /mnt"
os.system(cmd)
first, you should make sure your sshfs command works fine using the shell. Then, go to here to see many examples of using subprocess module of Python to call your sshfs commmand
import subprocess
mount_command = f'sshfs {host_username}#{host_ip}:{host_data_directory} {local_data_directory}'
subprocess.call(mount_command, shell=True)
# Do your stuff with mounted folder
unmount_command = f'fusermount -u {local_data_directory}'
subprocess.call(unmount_command, shell=True)