When I run this os.walk code from my PyScripter IDE, it works just fine. The os.walk was able to traverse the remote unix path and print the directory and file names.
However, when I run this same script from the cgi-bin of my Apache server, I get no os.walk (path, dirs, files) output. I also don't get any error messages recorded in the Apache error.log file.
Note: Python and my cgi-bin are on the same Windows machine, and the remote_path is on Unix.
Why does this same code work from the console but not from the cgi-bin and what can I do to resolve this?
#!C:\Python27\python.exe -u
import os
print "Content-type: text/html\n\n";
print "<html><head>"
print "<font size=+2><B>os.walk test</B></font><br><br>";
# Remote path
remote_path = r'\\unix_server\path\2015\q1\files\na\canada'
i = 0
for (path, dirs, files) in os.walk(remote_path):
print "Path", path
print "<BR><BR>Dirs", dirs
print "<BR><BR>Files", files
i += 1
if i >= 1:
break
Thanks to kindall, I found out the problem was indeed that Apache didn't have the appropriate permissions.
To solve the issue, I did the following (Windows 7 OS):
Opened services.msc. Start -> Run -> "services.msc"
Located my Apache service in the list "Apache2". Double click it.
In the Properties dialog for the Apache2 service, switch to the "Log On" tab. The radio button for "Local System account" was checked. I switched it to "This account" and clicked Browse.
In the "Enter object name to select" text box, I entered the name of my network + backslash + my user name. For example NETWORK\username. Clicked OK.
Then on the Log On tab, I saw that my full network email address was populated. I entered my password and password confirmation and clicked Apply.
The final steps were to stop the Apache2 service and then restart the service. Once that was done, the problem was solved and my script started working from the cgi-bin. :)
Related
The principle of the script is that it should connect from one virtual machine to another via an SSH connection and generate a certain number of folders.
The script runs, but when executed, generates folders on the host machine.
import os
from paramiko import SSHClient, AutoAddPolicy
from sys import argv
address = argv[1]
port = int(argv[2])
name = argv[3]
path = argv[4]
prefix = argv[5]
counts = int(argv[6])
mode = int(argv[7])
def generateFolders(path, prefix, counts, mode):
for i in range(1, counts+1):
folderName = prefix + str(i)
pth = os.path.join(os.path.expanduser('~'), path, folderName)
os.mkdir(pth, mode)
command = generateFolders(path, prefix, counts, mode)
print(address)
client1 = SSHClient()
client1.set_missing_host_key_policy(AutoAddPolicy())
client1.connect(address, username=name, password='1')
stdin, stdout, stderr = client1.exec_command(command)
print(stdout.read())
client1.close()
The command in the terminal
But without a script, I can connect to another virtual machine
Jane, its making dirs on your local box because that is where the python script is running.
I suggest you look at this question and answer.
In that QandA, they show how to use ssh on the local box to execute commands on a remote box. You could use your existing code as the code which is run on the remote box using the above as your guide.
Specifically this this one
The os.mkdir creates folders on the local machine. It won't magically start working on a remote machine only because you have previous opened SSH connection to that machine (and you actually even did not, as you open it only after calling os.mkdir).
To create a folder on a remote machine via Paramiko module, use SFTPClient.mkdir.
sftp = client1.open_sftp()
for i in range(1, counts+1):
folderName = prefix + str(i)
pth = os.path.join(os.path.expanduser('~'), path, folderName)
sftp.mkdir(pth, mode)
Though you should not use os.path on SFTP paths as your code will break, if run on Windows and other platforms, that does not use / as a path separator. And os.path.expanduser will of course expand ~ to local user home. I do not think you want that.
Obligatory warning: Do not use AutoAddPolicy on its own – You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".
I am trying to write a python script which can block websites based on the time of the day but i am not able to edit the hosts file in windows even running the cmd as an administrator.
Here's the Code:
import time
from datetime import datetime as dt
hosts_location=r"C:\Windows\System32\drivers\etc\hosts"
hosts_temp="hosts"
blocked_websites=['www.facebook.com','facebook.com']
redirect="127.0.0.1"
while True:
if dt(dt.now().year,dt.now().month,dt.now().day,8) < dt.now() <
dt(dt.now().year,dt.now().month,dt.now().day,17):
with open(hosts_location,"r+") as file:
content=file.read()
for website in blocked_websites:
if website in content:
pass
else:
file.write(redirect+"\t"+website+"\n")
print("Working Hours....")
else:
with open(hosts_location,"r+") as file:
content=file.readlines()
file.seek(0)
for line in content:
if not any(website in line for website in blocked_websites):
file.write(line)
file.truncate()
print("Fun Time...")
time.sleep(5)
Update:The code is working fine for a local hosts file but when i try it for the real hosts file even as administrator the script just flashes and with cmd shows message "[Errno 13]:Permission Denied".
Also i am not able to edit hosts even with notepad even as adminstrator my os is windows 10 home edition 64-bit.
First of all,
If you are not an administrator, change permissions of the hosts file by right clicking--> Properties --> Security and allow Read/Write/Execute Permissions for Users.
Then open Command Prompt as administrator and run the script. It will work!
try to save the python file as a .pyw extension
This will work for you.
Go to the hosts file and uncheck "read only" then go to security tab and give the permissions to user to read and write. Hope it'll work.
You should change access to file from users.
Go to C:\Windows\System32\drivers\etc and click right on properties, choose security and edit access to file.
Just change the path to be:
hosts_location = "C:\\Windows\\System32\\drivers\\etc\\hosts"
without r", and it will work
I wanted to create a desktop launcher for my Python application. The application executes various ssh operations over pexpect with publickey-authentication. The problem is however, when I start my app with the .desktop launcher it doesn't work properly. The ssh connections ask for a password and don't use the publickeys. But it works fine via commandline execution.
The .desktop File looks like this:
[Desktop Entry]
Version=1.0
Name=SSH-Manager
Comment=XYZ
Exec=python /home/userx/SSH-Manager/startup.py
Icon=/home/userx/SSH-Manager/resources/icon.png
Path=/home/userx/repos/SSH-Manager
Terminal=true
Type=Application
Categories=Utility;Application;
StartupNotify=false
The desktop environment is KDE and the desktop user is the same as the commandline user.
Can someone explain why I get such strange behavior with the launcher?
Edit: Example function
def run(self):
self.a_signal.emit("Retrieving Data")
try:
session = pxssh()
session.force_password = False
hostname = self.client
username = "root"
session.login(hostname, username)
session.sendline("ls -a")
session.prompt()
session.logout()
except ExceptionPxssh as e:
print ("pxssh failed: ")
self.error_signal.emit("failed", str(e))
print e
return
self.process_output()
self.finish_signal.emit("done")
As Mirosław Zalewski suspected in the comments, the problem was the ssh-agent was not running for the desktop-environment because ssh-add was initially used in the /etc/sources. Executing ssh-add in the X-users ~./profile therefore solves the problem.
I am writing a program in python on Ubuntu, to remove a file from remote machine(raspberrypi) by accessing it, connected with network.
For file selection I am using a command called askopenfilename.
But I am struggling in specifying the Path of RaspberryPi correctly.
Can you please guide me on how do I specify Raspberrypi path correctly?
IP = "192.168.2.34"
Username = "pi"
Password ="raspberry"
Path="/home/pi/python"
Below is my code
from tkFileDialog import askopenfilename
import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('192.168.2.34', username='pi', password='raspberry')
checkdir = "/home/pi/python"
name1= askopenfilename(title = "Select File For Removal", initialdir = checkdir)
stdin, stdout, stderr = client.exec_command('ls -l')
for line in stdout:
print '... ' + line.strip('\n')
client.close()
I am fairly sure the tk file dialog is only able to browse the Ubuntu machine (which it seems to run on) filesystem - not the RPi filesystem over SSH, so you will never see RPi directories.
if You can read the RPi directory listing there, so you could create your own file browser component or try to find an existing one that works over SSH.
Then again it looks like you are 'inventing the wheel again' (which of course is ok for learning purposes) - gFTP, FileZilla, many of the Linux desktop file browsers etc. (or WinSCP if you were using a Windows box) are ready made tools for this.
I've looked all around Google and its archives. There are several good articles, but none seem to help me out. So I thought I'd come here for a more specific answer.
The Objective: I want to run this code on a website to get all the picture files at once. It'll save a lot of pointing and clicking.
I've got Python 2.3.5 on a Windows 7 x64 machine. It's installed in C:\Python23.
How do I get this script to "go", so to speak?
=====================================
WOW. 35k views. Seeing as how this is top result on Google, here's a useful link I found over the years:
http://learnpythonthehardway.org/book/ex1.html
For setup, see exercise 0.
=====================================
FYI: I've got zero experience with Python. Any advice would be appreciated.
As requested, here's the code I'm using:
"""
dumpimages.py
Downloads all the images on the supplied URL, and saves them to the
specified output file ("/test/" by default)
Usage:
python dumpimages.py http://example.com/ [output]
"""
from BeautifulSoup import BeautifulSoup as bs
import urlparse
from urllib2 import urlopen
from urllib import urlretrieve
import os
import sys
def main(url, out_folder="C:\asdf\"):
"""Downloads all the images at 'url' to /test/"""
soup = bs(urlopen(url))
parsed = list(urlparse.urlparse(url))
for image in soup.findAll("img"):
print "Image: %(src)s" % image
filename = image["src"].split("/")[-1]
parsed[2] = image["src"]
outpath = os.path.join(out_folder, filename)
if image["src"].lower().startswith("http"):
urlretrieve(image["src"], outpath)
else:
urlretrieve(urlparse.urlunparse(parsed), outpath)
def _usage():
print "usage: python dumpimages.py http://example.com [outpath]"
if __name__ == "__main__":
url = sys.argv[-1]
out_folder = "/test/"
if not url.lower().startswith("http"):
out_folder = sys.argv[-1]
url = sys.argv[-2]
if not url.lower().startswith("http"):
_usage()
sys.exit(-1)
main(url, out_folder)
On windows platform, you have 2 choices:
In a command line terminal, type
c:\python23\python xxxx.py
Open the python editor IDLE from the menu, and open xxxx.py, then press F5 to run it.
For your posted code, the error is at this line:
def main(url, out_folder="C:\asdf\"):
It should be:
def main(url, out_folder="C:\\asdf\\"):
Usually you can double click the .py file in Windows explorer to run it. If this doesn't work, you can create a batch file in the same directory with the following contents:
C:\python23\python YOURSCRIPTNAME.py
Then double click that batch file. Or, you can simply run that line in the command prompt while your working directory is the location of your script.
Since you seem to be on windows you can do this so python <filename.py>. Check that python's bin folder is in your PATH, or you can do c:\python23\bin\python <filename.py>. Python is an interpretive language and so you need the interpretor to run your file, much like you need java runtime to run a jar file.
use IDLE Editor {You may already have it} it has interactive shell for python and it will show you execution and result.
Your command should include the url parameter as stated in the script usage comments.
The main function has 2 parameters, url and out (which is set to a default value)
C:\python23\python "C:\PathToYourScript\SCRIPT.py" http://yoururl.com "C:\OptionalOutput\"
If you want to run .py files in Windows, Try installing Git bash
Then download python(Required Version) from python.org and install in the main c drive folder
For me, its :
"C:\Python38"
then open Git Bash and go to the respective folder where your .py file is stored :
For me, its :
File Location : "Downloads"
File Name : Train.py
So i changed my Current working Directory From "C:/User/(username)/" to "C:/User/(username)/Downloads"
then i will run the below command
" /c/Python38/python Train.py "
and it will run successfully.
But if it give the below error :
from sklearn.model_selection import train_test_split
ModuleNotFoundError: No module named 'sklearn'
Then Do not panic :
and use this command :
" /c/Python38/Scripts/pip install sklearn "
and after it has installed sklearn go back and run the previous command :
" /c/Python38/python Train.py "
and it will run successfully.
!!!!HAPPY LEARNING !!!!