I´m using cron in a Raspberry Pi (Raspbian) to run a script in Python to upload a file every x minutes to my server, this file is created every x minutes from another script (which is loaded from cron every x-1 minutes). When running manually the upload.py it works well and the file is uploaded correctly, but when using cron to run upload.py, the uploaded file is always the same even if i delete it from the server.
Example:
-File in Raspberry Pi is 40kb or 100kb or 200kb.
-The file in Server is always 40kb. If i delete it from the server, the next time upload.py runs from cron, it will appear the same 40kb file even if the file in the Raspberry Pi is 100kb or 200kb.
Create file .py
import os
import shutil
with open("temp.dat", 'r') as fsrc:
with open("up.dat", 'w+') as fdest:
fsrc.seek(-324279, os.SEEK_END)
shutil.copyfileobj(fsrc, fdest)
fsrc.close()
fdest.close()
Upload .py
from ftplib import FTP
HOST = 'host.com'
FTP_NAME = 'username'
FTP_PASS = 'password'
ftp = FTP(HOST)
ftp.login(FTP_NAME, FTP_PASS)
file = open('up.dat', 'r')
ftp.storlines('STOR up.dat', file)
ftp.quit()
file.close()
Cant understand why this happen. Any help is welcome.
Thanks.
Related
I am attempting to make a program update itself to the newest version that I have made. E.g. I added a new functionality to it. It would be useful for me to be able to upload the updated file to a central location like my Raspberry Pi and have the program update itself across all of my computers without updating each one individually.
I have made the bellow code, but it does not work. It can recognize when the file is up-to-date but running the new program it downloads fails, it successfully downloads and deletes itself, but the new program is not run, with no error messages being shown.
Update test.py:
#updaterV1.py
import time
import requests
import os
import hashlib
time.sleep(5)
cwd = os.getcwd()
URL = r"http://[rasberry pi's ip]/update%20files/dev/hash.txt"
hash_path = os.path.join(cwd,"remote hash.txt")
with open (hash_path, "wb") as f:
f.write(requests.get(URL).content)
with open(hash_path,"r") as hash_file:
remotehash = (hash_file.readline()).strip()
os.remove(hash_path)
hasher = hashlib.sha256()
with open(__file__, 'rb') as self_file:
selfunhashed = self_file.read()
hasher.update(selfunhashed)
selfhash = hasher.hexdigest()
print(selfhash)
print(remotehash)
if (selfhash == remotehash):
print("program is up to date")
input()
else:
update_path = os.path.join(cwd,"temp name update.py")
URL = r"http://[rasberry pi's ip]/update%20files/dev/update.py"
with open (update_path, "wb") as f:
f.write(requests.get(URL).content)
with open(update_path,"r") as f:
name = f.readline().strip()
name = name[1:] #use the 1st line as "#name.py" not "# name"
update_path = os.path.join(cwd,name)
try:
os.remove(update_path)
except:
pass
os.rename(os.path.join(cwd,"temp name update.py"),update_path)
os.system("python \""+update_path+"\"")
print("removing self file now")
os.remove(__file__)
It uses a separate TXT file with the hash of the program stored in the same folder to check the remote files hash without downloading the actual file to hash it locally.
I am in final phase of publish a nice game in App Store. However, I am stuck with a problem. In my app there are json files which saves and loads game in every step. They all works fine in Google Play Store.
I have:
macOS Big Sur 11.6
Python 3.10.4
XCode Version 13.2.1
iPhone 7 iOS 14.7.1
I have build the Xcode file with kivy-ios toolchain.
I use those codes to read and write json files;
#TO READ THE DATA FROM kel1.json: !!WORKS FINE!!
file = open('kel1.json',"r",encoding='utf-8')
klist = json.load(file)
#TO CLEAN ALL THE DATA IN savelist1.json: !!GIVES ERROR!!
telal={}
json_objecti = json.dumps(telal)
with open("savelist2.json", "w") as outfile:
outfile.write(json_objecti).
#TO APPEND A DATA IN savelist1.json: !!GIVES ERROR!!
dict={aln:"item"}
file=open("savelist1.json", "r+",encoding='utf-8')
data = json.load(file)
data.update(dict)
file.seek(0)
json.dump(data, file,ensure_ascii=False)
as I said all above codes work fine when I turn that app to APK, AAB and after I publish the AAB to the GooglePlay.
Also when I run simulation in Xcode, it works fine as well. But when I connect my phone and run the app, "r" json works but "r+" and "w" gives error;
File "/Users/batuhan/programs/kivy-ios/2800-ios/YourApp/main.py", line 429, in secom
PermissionError: [Errno 1] Operation not permitted: '/private/var/containers/Bundle/Application/C2508051-03DA-40EC-8587-A40D8B922055/2800.app/YourApp/savelist1.json'
2022-05-19 20:14:09.322995+0300 2800[3486:1776834] Application quit abnormally!
2022-05-19 20:14:09.416628+0300 2800[3486:1776834] Leaving
I have found those and try;
How can I allow permissions for my Kivy app to access text files when running on an iOS device?
Permission denied to .json file when using a JSON file as a store in Kivy app
Python-Kivy on iOS (Xcode): Got dlopen error on Foundation: (...): image not found
Kivy: Error on iOS using JSON file for High Score
What have I did so far;
I gave all the permissions in security and privacy in 'full disk access', 'accessibility' and 'files and folders' for Xcode, python, terminal, finder.
I manually give read&write privilege for every folder, with every item inside them on folder info.
I have tried to JSONStore but because of encoding problem I have dropped it.
I have tried;
path1=os.path.dirname(os.path.abspath(__file__))
tela={}
json_object = json.dumps(tela, indent = 4)
file_path1=os.path.join(path1,"savelist1.json")
with open(file_path1, "w") as outfile:
outfile.write(json_object)
#THIS ONE WORKS FINE IN SIMULATOR BUT GIVES SAME ERROR IN PHONE AS WELL
I have tried;
tela={}
json_object = json.dumps(tela, indent = 4)
script_dir=os.path.dirname(__file__)
file_path1=os.path.join(script_dir, "savelist1.json")
with open(file_path1, "w") as outfile:
outfile.write(json_object)
#THIS ONE WORKS FINE IN SIMULATOR BUT GIVES SAME ERROR IN PHONE AS WELL
I have cleaned and build folder in Xcode at every step.
I have tried user_data_dir, but it returns the path to ~/Documents/<app_name> and json file is not there. Json files are in same folder with main.py and my.kv.
I still can't find the right method to update json file in iPhone.
Please help.
it is necessary to define the file location as specified here(https://kivy.org/doc/stable/api-kivy.app.html?highlight=user_data_dir#kivy.app.App.user_data_dir)
I'm appending values to a log file every 6th second. Every 30 sec I'm transferring this log to an FTP server as a file. But instead of transfering the whole file, I just want to append the collected data to the file on my server. I haven't been able to figure out how to open the server file and then append the values.
My code so far:
session = ftplib.FTP(authData[0],authData[1],authData[2])
session.cwd("//"+serverCatalog()+"//") # open server catalog
file = open(fileName(),'rb')
with open(fileName(), 'rb') as f:
f = f.readlines()
for line in f:
collected = line
# In some way open server file, write lines to it
session.storbinary('STOR ' + fileName(), open(fileName(), 'a'), 1024)
file.close()
session.quit()
Instead, do I have to download the server file open and append, then send it back?
Above was my question, the full solution is below:
session.cwd("//"+serverCatalog()+"//") # open server catalog
localfile = open("logfile.txt",'rb')
session.storbinary('APPE serverfile.txt', localfile)
localfile.close()
Use APPE instead of STOR.
Source: http://www.gossamer-threads.com/lists/python/python/22960 (link to web.archive.org)
I have this weird issue with my code on Raspberry Pi 4.
from gpiozero import CPUTemperature
from datetime import datetime
import ftplib
cpu = CPUTemperature()
now = datetime.now()
time = now.strftime('%H:%M:%S')
# Save data to file
f = open('/home/pi/temp/temp.txt', 'a+')
f.write(str(time) + ' - Temperature is: ' + str(cpu.temperature) + ' C\n')
# Login and store file to FTP server
ftp = ftplib.FTP('10.0.0.2', 'username', 'pass')
ftp.cwd('AiDisk_a1/usb/temperature_logs')
ftp.storbinary('STOR temp.txt', f)
# Close file and connection
ftp.close()
f.close()
When I have this code, script doesn't write anything to the .txt file and file that is transferred to FTP server has size of 0 bytes.
When I remove this part of code, script is writing to the file just fine.
# Login and store file to FTP server
ftp = ftplib.FTP('10.0.0.2', 'username', 'pass')
ftp.cwd('AiDisk_a1/usb/temperature_logs')
ftp.storbinary('STOR temp.txt', f)
...
ftp.close()
I also tried to write some random text to the file and run the script, and the file transferred normally.
Do you have any idea, what am I missing?
After you write the file, the file pointer is at the end. So if you pass file handle to FTP, it reads nothing. Hence nothing is uploaded.
I do not have a direct explanation for the fact the local file ends up empty. But the strange way of combining "append" mode and reading may be the reason. I do not even see a+ mode defined in open function documentation.
If you want to both append data to a local file and FTP, I suggest your either:
Append the data to the file – Seek back to the original position – And upload the appended file contents.
Write the data to memory and then separately 1) dump the in-memory data to a file and 2) upload it.
I'm trying to upload a file to my VPS (hosted by GoDaddy) via Python's ftplib library:
from ftplib import FTP
session = FTP('ftp.wangsibo.xyz','wsb','Wsb.139764')
file = open('source10.png','rb')
session.storbinary('store_source10.png', file)
file.close()
session.quit()
However it gets stuck at line 4 (the file is only a few k's and it's taking minutes). The same thing happens when I'm trying to read using retrbinary.
I've tried using FileZilla and it worked fine. Any suggestions?
FTP.storbinary(command, fp[, blocksize, callback, rest])
Store a file in binary transfer mode. command should be an appropriate
STOR command: "STOR filename". fp is an open file object which is read
until EOF using its read() method in blocks of size blocksize to
provide the data to be stored.
store_source10.png is not a command, you can try to use STOR source10.png.
e.g.
from ftplib import FTP
session = FTP('ftp.wangsibo.xyz','wsb','Wsb.139764')
file=open('source10.png','rb')
session.storbinary('STOR source10.png',file)
file.close()
session.quit()