Running batch command inside script to start SimpleHTTPServer not working properly - python

I'm trying to start SIMPLEHTTPSERVER from inside python via an os.system batch command to serve to a specific directory.
In a regular batch file, I have:
#echo off
pushd c:\MC\log
start C:\Python27\python.exe -m SimpleHTTPServer 8080
Which works great, but in Python if I use:
os.system('cmd /c pushd c:\MC\log start C:\Python27\python.exe -m SimpleHTTPServer 8080')
it starts the server, but seems to ignore the server directory.
Is it clear where I'm going wrong?

Related

one python3 script shoud start a second python3 script

i´ve a few problems with my python3 scripts.
an php script start an python3 script:
$comando = 'python3 /var/www/html/tmp/' . $usersession . '-newtenent-vcenter1.py';
shell_exec("/usr/bin/nohup ".$comando." >/dev/null 2>&1 &");
the python3 script write a few strings to an new created text file.
After all thinks are done, the script sould be start the next python3 script:
os.system('python3 /var/www/html/tmp/' + usersession + '-newtenent-cucm1.py')
BUT, python3 start the script "cucm1.py" and close it imeadlety! The script shoud be open an ssh session with paramiko.
The OS is an Ubuntu 18.x. I´ve added the www-data user to the script directory (so all scripts can be executed by the user www-data):
www-data ALL=(ALL) NOPASSWD: /usr/bin/python3 /var/www/html/
BUT, when we execute the first python3 script from the linux shell (as root) it work´s fine (the second script working fine).
any idea? THANK YOU!

Python script does not run after logging into computer remotely using "Screen"

Laptop A has python file "file1.py". Computer B has python file "file2.py". I want to remotely enter into Computer B and run the python script file2.py. I am using SCREEN, and below is my code.
import os
import time
os.system('screen -S Test -d -m /dev/ttyUSB0 57600')
time.sleep(1)
os.system('screen -S Test -X stuff "file2.py"')
time.sleep(1)
os.system('screen -S Test -d -r')
time.sleep(0.25)
print "done"
How did you try to run your 'remote' script?
You can try ssh session:
ssh user#computer_b 'python file2.py'
Of course, you should provide a full path to your file2.py script and maybe (I'm not 100% sure) a full path to a Python executable on your remote Computer B. Another option is to make your file2.py executable, by adding a Python "shebang line" as the first line of your file2.py script and setting executable bit via chmod +x file2.py:
Should I put #! (shebang) in Python scripts, and what form should it take?

Python Simple HTTP Server Batch File Error

I'm currently playing around with home networking and getting into servers and I found a python command that will make the directories of you python folder avaliable over your LAN. To execute it in windows, I've just been entering the following into Command Prompt:
cd..
cd..
cd Python33
python -m http.server 8000
This creates the server that can be accessed via x.x.x.x:8000. Instead of manually entering these commands every time, however, I tried to make a batch script that would do this automatically. Below is the script for the batch file. The problem I'm having, is When I run the batch file, command prompt is saying 'python' is not recognized as an internal or external command. I was wondering if someone could tell my why this is happening and offer a solution or reason as to why this couldn't work. Thanks.
#ECHO OFF
cd..
cd..
cd Python33
python -m http.server 8000
pause
EDIT:
Below is the code that works, this will also set the directory to the C drive.
#ECHO OFF
cd C:\
C:\Python33\python -m http.server 8000
Maybe it's because the batch file is located in a directory that is not 2- level deep (C:\path\to\batch.bat)
Specifying the path of the python executable will solve your problem.
#ECHO OFF
C:\python33\python -m http.server 8000
pause
Try this:
#ECHO OFF
cd..
cd..
cd Python33
python -m SimpleHTTPServer 8000
pause

python - running ssh/rsync commands in a script called via crontab

I have a very strange issue that I can't seem to figure out.
When I execute a python script containing the following lines while inside a SSH terminal (putty), it works fine. But the moment I run the script via crontab or even nohup python myscript >/dev/null 2>&1& it doesn't seem to execute these commands.
subprocess.call('rsync -avr /path/to/folder/. --include "delta.*" --exclude "*" -e "ssh -o StrictHostKeyChecking=no -i /path/to/key.pem" ec2-user#'+server+':/path/to/folder/', shell=True)
local('ssh -t -o StrictHostKeyChecking=no -i /path/to/key.pem ec2-user#'+server+' "sudo /usr/bin/indexer -c /path/to/sphinx.conf --merge main delta --rotate"')
Basically all the above is doing is syncing a folder with new sphinx search engine updates to a remote server, then the second line runs a remote ssh command to force the search engine to rotate updates into production.
I do have fabric installed (hence the local command) but to avoid having to fab a second file I was hoping a single line of code could allow me to execute sudo commands on a remote server.
Can someone help me out?
I found the answer, for ssh commands in a script run in the background, you need to to have -t -t to force a pseudo terminal.
Reference:
Pseudo-terminal will not be allocated because stdin is not a terminal

How to host python cgi script with `python -m SimpleHTTPServer 8000` or `python -m CGIHTTPServer 8000`?

When I run python -m SimpleHTTPServer 8000 or python -m CGIHTTPServer 8000 in my shell I am hosting the content of my current directory to the internet.
I would like to make the following cgi_script.py work correctly using the above command in the command line when I browse to 192.xxx.x.xx:8000/cgi_script.py
#!/usr/bin/env python
print "Content-Type: text/html"
print
print """\
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
"""
But this script is displayed literally and not only the "Hello World!" part.
Btw I changed the file permissions to 755 for cgi_script.py as well as the folder I am hosting it from.
Try with python -m CGIHTTPServer 8000.
Note that you have to move the script to a cgi-bin or htbin directory in order to be runnable.
SO doesn't allow me to comment so I'm adding this as a separate answer, addition to rodrigo's.
You can use another parameter cgi_directories which defaults to ['/cgi-bin', '/htbin']. More info here
In Python3 the command line is simply
python3 -m http.server --cgi 8000
When I ran into this issue I found that depending on which directory you are in when you run the python -m CGIHTTPServer 8000 command yields different results. When attempting to run the command while in the cgi-bin directory the browser continued to return the raw script code. once I cd'ed one level higher and ran the python -m CGIHTTPServer 8000 command again my script began executing.
#Bentley4 -ifyou are still not able to do,
try importing cgi.
#!C:\Python34\python.exe -u
import cgi
print ("Content-type:text/html")
HTH
This work for me, run the python -m CGIHTTPServer 8000 command same menu level with cgi-bin,and move cgi_script.py into cgi-bin folder.In browser type http://localhost:8000/cgi-bin/cgi_script.py

Categories

Resources