Sending files between computers remotely [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
How can I send and receive files remotely, and also push updates via python? We have a bunch of devices out in the market and they are all Windows ten based. How could we go about sending files to those machines and receive files from those machines? We would like to use python for this task. Any tutorials or articles could be awesome.

I wrote this script a while ago to send files to my remote SFTP server from my local laptop. The machines has each other's public keys:
import pysftp
import paramiko
fpaths = ['list/of', 'file/paths']
with pysftp.Connection(server, username='loginID') as sftp:
with sftp.cd('target/directory'):
for fpath in fpaths:
print("Sending:", fpath)
if not os.path.isdir(fpath):
sftp.put(fpath)
print("Permissioning", fpath)
sftp.chmod(os.path.basename(fpath), 755)
else:
dirname = os.path.basename(fpath)
if not sftp.isdir(dirname):
sftp.mkdir(dirname)
print("Permissioning", dirname)
sftp.chmod(os.path.basename(dirname), 755)
sftp.put_r(fpath, dirname)
sftp.walktree(dirname,
dcallback=lambda dname:print("Permissioning", dname) or sftp.chmod(dname, 755),
fcallback=lambda fname:print("Permissioning", fname) or sftp.chmod(fname, 755),
ucallback=lambda x:x)

Try using ftplib package for python ftp connection. Here is the small tutorial for that.
import ftplib
ftp = ftplib.FTP("www.python.org")
ftp.login("anonymous", "ftplib-example-1")
data = []
ftp.dir(data.append)
ftp.quit()
for line in data:
print "-", line
Executing above code example:
$ python ftplib-example-1.py
- total 34
- drwxrwxr-x 11 root 4127 512 Sep 14 14:18 .
- drwxrwxr-x 11 root 4127 512 Sep 14 14:18 ..
- drwxrwxr-x 2 root 4127 512 Sep 13 15:18 RCS
- lrwxrwxrwx 1 root bin 11 Jun 29 14:34 README -> welcome.msg
- drwxr-xr-x 3 root wheel 512 May 19 1998 bin
- drwxr-sr-x 3 root 1400 512 Jun 9 1997 dev
- drwxrwxr-- 2 root 4127 512 Feb 8 1998 dup
- drwxr-xr-x 3 root wheel 512 May 19 1998 etc
...
Else, you may go with the SSH using Paramiko. Use whichever suits you better.
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
ssh.connect('127.0.0.1', username='none',
password='lol')
Ftplib code reference: The ftplib module
Paramiko code reference: SSH PROGRAMMING WITH PARAMIKO | COMPLETELY DIFFERENT

Related

Why I canot import _crop_and_resize in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
I have built _crop_and_resize.so by below c++ code from here,
sources = ['src/crop_and_resize.c']
headers = ['src/crop_and_resize.h']
extra_compile_args = ['-fopenmp', '-std=c99']
sources = [os.path.join(this_file, fname) for fname in sources]
headers = [os.path.join(this_file, fname) for fname in headers]
extra_objects = [os.path.join(this_file, fname) for fname in extra_objects]
ffi = create_extension(
'_ext.crop_and_resize',
headers=headers,
sources=sources,
define_macros=defines,
relative_to=__file__,
with_cuda=with_cuda,
extra_objects=extra_objects,
extra_compile_args=extra_compile_args)
After compilation, I am trying to import "_crop_and_resize.so" in python, but I am receiving an error.
>>> import _crop_and_resize as _backend
the error:
ImportError:_crop_and_resize.so: file too short
specifications:
g++ (GCC)= 9.3.0
python= 3.6.10
Ubuntu 20.04.4 LTS 64bit
Per #AMIRABBAS's comment, the output of stat _crop_and_resize.so on the Ubuntu 20.04.4 LTS, 64 bit terminal is:
File: _crop_and_resize.so
Size: 0 Blocks: 40 IO Block: regular empty file
Device: Inode: Links: 1
Access: (0640/-rw-r-----) Uid: (000000/ my username) Gid:
(000000/ my username)
Access: 2022-05-11 10:12:48.000000000 -0700
Modify: 2022-05-11 10:51:27.000000000 -0700
Change: 2022-05-11 10:51:27.000000000 -0700
For whatever reason, when downloading the repository, it did not clone the file directly. Needless to say, you cannot link to a zero-length file.
For reference, having downloaded the same file directly from GitHub, the output should be:
~/downloads> stat _crop_and_resize.so
File: _crop_and_resize.so
Size: 101304 Blocks: 200 IO Block: 4096 regular file
Device: 19h/25d Inode: 5602044 Links: 1
Access: (0644/-rw-r--r--) Uid: (1000/me) Gid: (1000/me)
Access: 2022-05-11 14:24:56.233800387 -0400
Modify: 2022-05-11 14:22:48.968012681 -0400
Change: 2022-05-11 14:24:53.433609539 -0400
Birth: 2022-05-11 14:22:48.940010253 -0400
Update:_crop_and_resize.so was empty, and I successfully downloaded the file, "_crop_and_resize.so" from its repository, and run it again and the current error gone.

Raspberry Pi (Raspbian Linux flavor) run script on wifi up

Currently I am experience issues with the script automatic run after wifi adapter connects to a network.
After ridiculously extended research, I've made several attempts to add script to a /etc/network/if-up.d/. Manually my script works; however it does not automatically.
User permissions:
ls -al /etc/network/if-up.d/*
-rwxr-xr-x 1 root root 703 Jul 25 2011 /etc/network/if-up.d/000resolvconf
-rwxr-xr-x 1 root root 484 Apr 13 2015 /etc/network/if-up.d/avahi-daemon
-rwxr-xr-x 1 root root 4958 Apr 6 2015 /etc/network/if-up.d/mountnfs
-rwxr-xr-x 1 root root 945 Apr 14 2016 /etc/network/if-up.d/openssh-server
-rwxr-xr-x 1 root root 48 Apr 26 03:21 /etc/network/if-up.d/sendemail
-rwxr-xr-x 1 root root 1483 Jan 6 2013 /etc/network/if-up.d/upstart
lrwxrwxrwx 1 root root 32 Sep 17 2016 /etc/network/if-up.d/wpasupplicant -> ../../wpa_supplicant/ifupdown.sh
Also, I've tried to push the command directly in /etc/network/interfaces
by adding a row
post-up /home/pi/r/sendemail.sh
Contents of sendemail.sh:
#!/bin/sh
python /home/pi/r/pip.py
After the reboot, nothing actually happen. I've even tried sudo in front
I assume that wpasupplicant is the thing which causes that, but I cannot get how to run my script in ifupdown.sh script under /etc/wpa_supplicant.
Appreciate your help!
If you have no connectivity prior to initializing the wifi interface, I would suggest adding a cron job of a bash or python script that checks for connectivity every X minutes.
Ping (host);
If host is up then run python commands or external command.
This is rather ambiguous but hopefully is of some help.
Here is an example of a script that will check if a host is alive;
import re,commands
class CheckAlive:
def __init__(self):
myCommand = commands.getstatusoutput('ping ' + 'google.com)
searchString = r'ping: unknown host'
match = re.search(searchString,str(myCommand))
if match:
# host is not alive
print 'no alive, don't do stuff';
else:
# host is alive
print 'alive, time do stuff';

Python service - writing filename with timestamp

I wrote a Python script that will run indefinitely. It monitors a directory using PyInotify and uses the Multiprocessing module to run any new files created in those directories through an external script. That all works great.
The problem I am having is writing the output to a file. The filename I chose uses the current date (using datetime.now) and should, theoretically, roll on the hour, every hour.
now = datetime.now()
filename = "/data/db/meta/%s-%s-%s-%s.gz" % (now.year, now.month, now.day, now.hour)
with gzip.open(filename, 'ab') as f:
f.write(json.dumps(data) + "\n")
f.close() #Unsure if I need this, here for debug
Unfortunately, when the hour rolls on -- the output stops and never returns. No exceptions are thrown, it just stops working.
total 2.4M
drwxrwxr-x 2 root root 4.0K Sep 8 08:01 .
drwxrwxr-x 4 root root 12K Aug 29 16:04 ..
-rw-r--r-- 1 root root 446K Aug 29 16:59 2016-8-29-16.gz
-rw-r--r-- 1 root root 533K Aug 30 08:59 2016-8-30-8.gz
-rw-r--r-- 1 root root 38K Sep 7 10:59 2016-9-7-10.gz
-rw-r--r-- 1 root root 95K Sep 7 14:59 2016-9-7-14.gz
-rw-r--r-- 1 root root 292K Sep 7 15:59 2016-9-7-15.gz #Manually run
-rw-r--r-- 1 root root 834K Sep 8 08:59 2016-9-8-8.gz
Those files aren't really owned by root, just changed them for public consumption
As you can see, all of the files timestamps end at :59 and the next hour never happens.
Is there something that I should take into consideration when doing this? Is there something that I am missing running a Python script indefinitely?
After taking a peek. It seems as if PyInotify was my problem.
See here (https://unix.stackexchange.com/questions/164794/why-doesnt-inotifywatch-detect-changes-on-added-files)
I adjusted your code to change the file name each minute, which speeds up debugging quite a bit and yet still tests the hypothesis.
import datetime
import gzip, time
from os.path import expanduser
while True:
now = datetime.datetime.now()
filename = expanduser("~")+"/%s-%s-%s-%s-%s.gz" % (now.year, now.month, now.day, now.hour, now.minute)
with gzip.open(filename, 'a') as f:
f.write(str(now) + "\n")
f.write("Data Dump here" + "\n")
time.sleep(10)
This seems to run without an issue. Changing the time-zone of my pc was also picked up and dealt with. I would suspect, given the above, that your error may lie elsewhere and some judicious debug printing of values at key points is needed. Try using a more granular file name as above to speed up the debugging.

Python script not working in linux

I have this python script
#!/usr/bin/env python
import datetime, os
from time import gmtime, strftime
to_backup = "/home/vmware/tobackup"
var1 = datetime.datetime.now().strftime('%b-%d-%I%p')
for f in os.listdir(to_backup):
if(os.path.isfile(f)):
print f + " is a file"
if(os.path.isdir(f)):
print f + " is a directory"
It is giving me empty ouput. i don't know where is the problem
OUTPUT FOR dr jimbob answer
total 36
-rwxrwxr-x 1 vmware vmware 440 May 5 07:41 back.py
-rwxrwxr-x 1 vmware vmware 2624 May 4 20:35 backup.sh
drwxr-xr-x 2 vmware vmware 4096 Jun 22 2010 Desktop
drwxrwxr-x 2 vmware vmware 4096 May 5 03:51 destination
drwxr-xr-x 2 root root 4096 May 4 18:49 public_html
drwxrwxr-x 2 vmware vmware 4096 May 1 07:47 python
-rwxrwxr-x 1 vmware vmware 560 May 1 13:20 regex.py
drwxrwxrwx 7 vmware vmware 4096 May 5 03:50 tobackup
total 20
drwxrwxrwx 2 vmware vmware 4096 May 5 03:50 five
drwxrwxrwx 2 vmware vmware 4096 May 5 03:50 four
drwxrwxrwx 2 vmware vmware 4096 May 5 03:50 one
drwxrwxrwx 2 vmware vmware 4096 May 5 03:50 three
drwxrwxrwx 2 vmware vmware 4096 May 5 03:50 two
Ok you have permission, but you aren't in the right directory when you list through the files. list_dir gives you a list of dirs/files without their path, and os.path.isfile('one') and os.path.isdir('one') will check whether the directory 'one' exists in the current directory (wherever you launched the script from, unless you explicitly changed directory with os.chdir or included the path, e.g., os.path.isdir('/home/vmware/tobackup/one').
#!/usr/bin/env python
import datetime, os
from time import gmtime, strftime
import subprocess
to_backup = "/home/vmware/tobackup"
var1 = datetime.datetime.now().strftime('%b-%d-%I%p')
os.chdir(to_backup)
# os.listdir(to_backup) = ['one', 'two', 'three', 'four', 'five']
for f in os.listdir(to_backup):
if(os.path.isfile(f)):
print f + " is a file"
if(os.path.isdir(f)):
print f + " is a directory"
or
to_backup = "/home/vmware/tobackup"
var1 = datetime.datetime.now().strftime('%b-%d-%I%p')
# os.listdir(to_backup) = ['one', 'two', 'three', 'four', 'five']
for f in os.listdir(to_backup):
if(os.path.isfile(os.path.join(to_backup,f))):
print f + " is a file"
if(os.path.isdir(os.path.join(to_backup,f))):
print f + " is a directory"
or with walk (but not actually walking through subdirs).
to_backup = "/home/vmware/tobackup"
var1 = datetime.datetime.now().strftime('%b-%d-%I%p')
root, dirs, files in os.walk(to_backup).next()
for file in files:
print f + " is a file in " + root
for dir in dirs:
print f + " is a directory"
EDIT: To be even clearer, the mistake with your original script is you have a file structure like:
/home/user/bin/your_script.py
/home/vmware/tobackup/
/home/vmware/tobackup/one
/home/vmware/tobackup/two
...
When you go to /home/user/bin to run your script (e.g., python your_script.py), os.listdir('/home/vmware/tobackup') gives you a list of file and dir names in /home/vmware/tobackup, that is ['one','two', ...]. However, when you do os.path.isfile('one') from the directory /home/user/bin, you check to see if /home/user/bin/one is a file, not whether /home/vmware/tobackup/one is a file. Since /home/user/bin/one doesn't exist, you get no output.
It works on my machine (ubuntu 10.10)
Maybe /home/vmware/tobackup is empty or you have no permissions to read it.
os.listdir(dir_name) returns filenames relative to the named directory. To use those in other commands, you need to either prepend the directory name (via f = os.path.join(to_backup, f) at the start of the loop body) or else change the working directory to the backup directory before starting the loop.
These are the first two alternatives shown in dr jimbob's answer.

How to get path of Start Menu's Programs directory?

...for current user? for all users?
I'm working an a small program which needs to create links in the start menu. Currently I'm hardcoding like below, but it only works in english locales, for example it should be "Startmenü" in german. What are cleaner, more portable approaches?
OUR_STARTMENU = os.environ['ALLUSERSPROFILE'] + '\Start Menu\Programs\Our Stuff'
thank you
I've heard of 2 ways of doing this. First:
from win32com.shell import shell
shell.SHGetSpecialFolderPath(0,shellcon.CSIDL_COMMON_STARTMENU)
Second, using the WScript.Shell object (source : http://www.mail-archive.com/python-win32#python.org/msg00992.html):
import win32com.client
objShell = win32com.client.Dispatch("WScript.Shell")
allUserProgramsMenu = objShell.SpecialFolders("AllUsersPrograms")
userMenu = objShell.SpecialFolders("StartMenu")
Another source: http://blogs.msdn.com/saveenr/archive/2005/12/28/creating-a-start-menu-shortcut-with-powershell-and-python.aspx
Also, CSIDL_COMMON_STARTMENU is for all user startup and CSIDL_STARTMENU for current user startup.
A friend, Luke Pinner of Environment.gov.au, gave a solution by email which uses a core module (python 2.5+). Believed to be multi-lingual as the return from the API call is unicode. Tested on Win7 with Japanese locale, and on another us-english machine by manually changing Start Menu to point to %USERPROFILE%\Startmenü
''' Get windows special folders without pythonwin
Example:
import specialfolders
start_programs = specialfolders.get(specialfolders.PROGRAMS)
Code is public domain, do with it what you will.
Luke Pinner - Environment.gov.au, 2010 February 10
'''
#Imports use _syntax to mask them from autocomplete IDE's
import ctypes as _ctypes
from ctypes.wintypes import HWND as _HWND, HANDLE as _HANDLE,DWORD as _DWORD,LPCWSTR as _LPCWSTR,MAX_PATH as _MAX_PATH, create_unicode_buffer as _cub
_SHGetFolderPath = _ctypes.windll.shell32.SHGetFolderPathW
#public special folder constants
DESKTOP= 0
PROGRAMS= 2
MYDOCUMENTS= 5
FAVORITES= 6
STARTUP= 7
RECENT= 8
SENDTO= 9
STARTMENU= 11
MYMUSIC= 13
MYVIDEOS= 14
NETHOOD= 19
FONTS= 20
TEMPLATES= 21
ALLUSERSSTARTMENU= 22
ALLUSERSPROGRAMS= 23
ALLUSERSSTARTUP= 24
ALLUSERSDESKTOP= 25
APPLICATIONDATA= 26
PRINTHOOD= 27
LOCALSETTINGSAPPLICATIONDATA= 28
ALLUSERSFAVORITES= 31
LOCALSETTINGSTEMPORARYINTERNETFILES=32
COOKIES= 33
LOCALSETTINGSHISTORY= 34
ALLUSERSAPPLICATIONDATA= 35
def get(intFolder):
_SHGetFolderPath.argtypes = [_HWND, _ctypes.c_int, _HANDLE, _DWORD, _LPCWSTR]
auPathBuffer = _cub(_MAX_PATH)
exit_code=_SHGetFolderPath(0, intFolder, 0, 0, auPathBuffer)
return auPathBuffer.value

Categories