ftp causing invalid syntax

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
I have built a motion sensor detector using this code: from the  
dronebotworkshop on youtube. While it works very well for me, I now want  
to upload the pictures to my NAS server.
I googled this and found this line of code:

curl -T /home/pi/filename.jpg  
ftp://ftp-host-machine//yourwebsite/folder/filename.jpg --user

Now when I run the python script:

  from picamera import PiCamera
import time
import datetime

# Create object for PIR Sensor
# PIR Sensor is on GPIO-4 (Pin 7)
pir = MotionSensor(4)

# Create Object for Camera
camera = PiCamera()

# Function to create new Filename from date and time
def getFileName():
return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")


while True:
# Get a Filename
filename = getFileName()
# Wait for a motion to be detected
pir.wait_for_motion
# Print text to Shell
print("Jellybean thief detected!")
# Preview camera on screen during video
camera.start_preview()
# Start recording video
camera.start_recording(filename)
# Record for 10 seconds
camera.wait_recording(10)
# Stop preview and recording
camera.stop_preview()
camera.stop_recording()
curl -T /home/pi/filename.jpg ftp://ftp-ipaddressof NAS  
server/mnt/folder/filename.jpg --user
# Wait 25 seconds and repeat
time.sleep(25)

It returns Invalid syntax at the ftp://ftp- etc

  I can get as far as a login box from my Ubuntu desktop with this:  
ftp://server ip address/mnt/Folder/subfolder, but it fails completely on  
the PiZero with invalid synatx at ftp

Re: ftp causing invalid syntax
On 14/05/2018 20:23, RobH wrote:
Quoted text here. Click to load it
[snip]

Quoted text here. Click to load it

curl is not a Python command but an external program. You either need to  
use pycurl (a la import pycurl) or you want to get your Python script to  
execute the curl program using os.system(....) or similar.

Much more simple would be to use the ftp library included in the  
standard Python library and do the ftp yourself. YMMV.








Re: ftp causing invalid syntax
On 14/05/18 20:42, mm0fmf wrote:
Quoted text here. Click to load it

Quoted text here. Click to load it

Quoted text here. Click to load it
Thanks, but when the python script runs without the inserted curl line,  
it takes pictures of whatever is sensed and writes the actual file to  
the sdcard.

I read somewhere that writing to the Pizero sdcard so many times, as it  
would, is not a could idea as it could be corrupted.

So I looked for a way to upload the jpg file to my server.

Re: ftp causing invalid syntax
On 14/05/2018 20:49, RobH wrote:
Quoted text here. Click to load it

Well curl is a program not a Python method. So you have to tell Python  
to execute curl with os.system() or similar or you can use the curl  
library pycurl or use ftplib. An example for Python2 on Windows, edit to  
fit.

import ftplib
from ftplib import FTP

File2Send = "V://GIS//Maps//County//11x17shd//2010BEAVER11x17shd.pdf"
Output_Directory = "//data2//ftp//pub//download//maps//"

try:
     ftp = FTP("XXX.XXX.XXX.XXX")
     ftp.login(username, password)
     file = open(File2Send, "rb")
     ftp.cwd(Output_Directory)
     ftp.storbinary('STOR ' + File2Send, file)
     print "STORing File now..."
     ftp.quit()
     file.close()
     print "File transfered"
else:
     print "An error occured"


See https://community.esri.com/thread/16333




Re: ftp causing invalid syntax
On 14/05/18 22:20, mm0fmf wrote:
Quoted text here. Click to load it

Quoted text here. Click to load it

Quoted text here. Click to load it









Quoted text here. Click to load it

Quoted text here. Click to load it
Thanks for the above but when I use import ftplib, it returns
bash: import: command not found.



Re: ftp causing invalid syntax
Quoted text here. Click to load it









Quoted text here. Click to load it

Quoted text here. Click to load it

You ran the script with Bash instead of Python, then.

Perhaps this would go more smoothly if you learned some Python?

--  
https://www.greenend.org.uk/rjk/

Re: ftp causing invalid syntax
On 15/05/18 20:30, Richard Kettlewell wrote:
Quoted text here. Click to load it

And I thought I was the grumpy old man...

--  
There?s a mighty big difference between good, sound reasons and reasons  
that sound good.

We've slightly trimmed the long signature. Click to see the full one.
ftp causing invalid syntax
 On 2018 May 17 05:38:56, you wrote to Richard Kettlewell:

 >>> Thanks for the above but when I use import ftplib, it returns
 >>> bash: import: command not found.
 >> You ran the script with Bash instead of Python, then.
 >>
 >> Perhaps this would go more smoothly if you learned some Python?

 TP> And I thought I was the grumpy old man...

by far, you are not the only grumpy old man here ;)

)\/(ark

Always Mount a Scratch Monkey
Do you manage your own servers? If you are not running an IDS/IPS yer doin' it
wrong...
... Fight Crime: Shoot a politician

Re: ftp causing invalid syntax
Quoted text here. Click to load it

Not being grumpy, just suggesting learning a bit about the complex tool
before using it.

--  
https://www.greenend.org.uk/rjk/

Re: ftp causing invalid syntax
following:


Quoted text here. Click to load it

    Unless your JPG fits entirely within the system RAM, you still have to
write the file to the SD card. "curl" and pretty much every other FTP
option requires files stored on the file system in order to read/transfer
them -- so no saving of the SD card, you still are writing to it.

    If the JPG does fit in the RAM and is not written to SD card by the
capture process, you could (with effort) feed the RAM copy to a programmed
FTP transfer. Python's ftplib .storbinary() expects "an open file
object"... I suspect anything that provides file-like I/O operations might
be usable -- like using the StringIO library for the JPG data (but again,
this assumes the camera capture can be to a string buffer and not to a file
on the SD card). {"pycurl" is not part of the standard library, you'd have
to install it}

Quoted text here. Click to load it

    You are using JPG for a video? Not MJPEG or JPEG XS? To my knowledge,
JPeG (joint photographic experts group) is a still image format. Note that
your filename format has an .h264 extension, which is likely compatible
with AVCHD (advanced video codec for high definition) MPEG (motion picture
experts group).

    However -- THAT is the statement that specifies where the capture is
being stored. And therefore, that is the statement that needs to change to
avoid writing to the SD card.

Quoted text here. Click to load it

    There are so many things wrong with that line... Even ignoring that
"curl" is an external program and not Python statement.

a)    You need to plug in the actual file name used in the capture command;
there is no "/home/pi/filename.jpg" being created

b)    What is that "ftp-ipaddressof NAS ..." supposed to represent? Those
added spaces are going to play hob with parsing of the command.

c)    If you are trying to save that file to a local network (NAS -- network
attached storage) server, you don't need FTP... The NAS storage should have
been mounted somewhere in the Linux filesystem associated with NFS protocol
(or would show up with a "drive letter" in Windows). Just create your
filename with the full path to the NAS mount point/directory you want to
use for saving videos.

https://en.wikipedia.org/wiki/Network-attached_storage

https://en.wikipedia.org/wiki/Network_File_System#Typical_implementation
"""
5    The client machine requests access to exported data, typically by
issuing a mount command. (The client asks the server (rpcbind) which port
the NFS server is using, the client connects to the NFS server (nfsd), nfsd
passes the request to mountd)
6    If all goes well, users on the client machine can then view and
interact with mounted filesystems on the server within the parameters
permitted.
"""

    Using direct reference to a NAS mounted directory avoids ever writing
to the SD card... And if you were going to use FTP you have a network
connection (wired or WiFi) so you could have a mounted NAS directory.



--  
    Wulfraed                 Dennis Lee Bieber         AF6VN
     snipped-for-privacy@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

Re: ftp causing invalid syntax
On 14/05/2018 23:16, Dennis Lee Bieber wrote:
Quoted text here. Click to load it

A PiZero has 512MB does it not... even with Linux and some stuff running  
there's going to be c. 400MB of RAM available which would allow for a  
fairly big file to be stored in RAM.

Whilst that script was storing the file on the SD card and thus wearing  
it out, it's a place to start. Get it going then use some kind of tmpfs  
in RAM so the SD card isn't touched once you have the bugs more or less  
ironed out.


Re: ftp causing invalid syntax
On 14/05/18 23:35, mm0fmf wrote:
Quoted text here. Click to load it

I'm sorry but you have lost me with the above statements

Re: ftp causing invalid syntax
Dennis Lee Bieber wrote:
Quoted text here. Click to load it

One jpeg will *easily* fit in system RAM. Also it?s in the filesystem.
Write to RAM at /run/shm/ and avoid all sd card writes. (Unless there?s
heavy swapping.)

Re: ftp causing invalid syntax
declaimed the following:

Quoted text here. Click to load it

    A true JPEG would... But closer look at the provided code indicates it
is not a JPEG, but a variant of h.264 VIDEO... That's likely to get a bit
larger <G>... Depends upon resolution and frame rate (which aren't
specified in the code -- the V2 Pi Camera module is 8MP as stills, and
supports 1080p30 video. I've not found a good source for estimating file
size -- Wikipedia seems to show around 20-50kbps for those resolutions...
Which should still hold 10 seconds worth of video (but note that the
original code does not seem to ever delete anything, it just creates new
time-stamped 10-second files with 25-second skips as long as motion
continues to be detected).

    However, given the OP's statement of trying to put the files on a NAS
device -- it should be possible to directly address the NAS storage and
touch neither the SD card nor RAM; and that avoids the complication of
having to delete the file(s) after transfer. I don't know how much overhead
the NAS access would inflict upon the camera capture... 50kbps shouldn't
have much of a hit on WiFi or 100Mbps Ethernet. If it does, using RAM for
the capture, and using the 25 second gap to do a transfer/delete may be
viable.

    I'd probably also change the OP's delay logic by capturing the start
time after motion is detected and then, after capture (~10 seconds) and
post-processing (assuming RAM to NAS/delete RAM), using initial capture
time + (say 30 seconds) - current time to determine the sleep time before
checking for motion again. This would result in evenly spaced clips as long
as there is motion... OP's code, if there were not post-capture processing,
is resulting in ~35 second spacing...


--  
    Wulfraed                 Dennis Lee Bieber         AF6VN
     snipped-for-privacy@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

Re: ftp causing invalid syntax
On 15/05/18 02:17, Dennis Lee Bieber wrote:
Quoted text here. Click to load it

The main problem I have is that ftp returns an invalid syntax.

If there some other way of uploading the pictures automatically , I  
would certainly try it.

Re: ftp causing invalid syntax
On 15/05/18 00:12, A. Dumas wrote:
Quoted text here. Click to load it

ok, but where would I do that in the python script which takes the picures.

Re: ftp causing invalid syntax
On 14/05/18 23:16, Dennis Lee Bieber wrote:
Quoted text here. Click to load it
I found the said line or information here:
https://www.newth.net/mars/how-to-set-up-a-raspberry-pi-zero-to-upload-images-to-a-server-via-ftp/

And I used the 2nd line first, then both lines, and still it returned  
the same error message: invalid syntax.

In answer to the b) the ip address of the NAS server means I actually  
use an ip address to access my NAS server, 192.168.x.x as an example.  
There would not be any added spaces when it is used.

When you say: Just create your filename with the full path to the NAS  
mount point/directory you want to use for saving videos.

Do you then mean to use an exact file name to move or upload to the NAS  
mount point.
This would be impractical I think because each picture has a date in the  
name, ie: 2018-05-07_22.40.54.jpg

Re: ftp causing invalid syntax
following:

Quoted text here. Click to load it

    Read the paragraph above those lines "To run an automated process in
Linux you need a shell script"... Shell scripts are run in the command
line, they are NOT Python scripts (you /start/ Python from a command line,
but then /it/ runs the Python script).

    You really need to look at a few introductions to Linux, BASH (shell),
and Python, because the impression you are presenting is that of someone
who thinks all of them work at the same level. Especially with the other
post where you were entering
    import ftplib
at a shell prompt, when /that/ is a Python statement.

Quoted text here. Click to load it

    Show us an example of the command you would use to create or open a
file on that NAS... Normally, in Linux, the IP or hostname would appear in
the mount command, or in an fstab entry, and the mounted path would not use
the IP/hostname itself (this allows the IP/hostname to change in the future
while the mount point stays the same, and programs are not affected).
.
Quoted text here. Click to load it

    And here again you are specifying a still-image JPEG, but your Python
script is making an H264 video... Different type of file, different
extension.

    What difference does the file name make? You generated the name, you
can reuse the name in multiple statements.

{Obviously untested -- I don't have a picamera, and I don't have a NAS}
{Save as "CameraExample.py", run by enterings "python CameraExample.py" at
the command line}
{Watch out for line wrap on long comments; and don't mess up the
indentation}
-=-=-=-=-
from picamera import PiCamera
import time
import os
import os.path
import shutil

from gpiozero import MotionSensor   #According to Google MotionSensor is in
gpiozero

pir = MotionSensor(4)

camera = PiCamera()

while True:
    pir.wait_for_motion()
    triggerTime = time.time()

    #create base file name at time of motion trigger
    fname = time.strftime("%Y-%m-%d_%H.%M.%S.h264",
                          time.localtime(triggerTime))
    RAMname = os.path.join("/run/shm", fname)
    NASname = os.path.join("/path/to/NAS/mount/point", fname)
    
    camera.start_preview()

    #capture video to RAM device first
    camera.start_recording(RAMname)
    camera.wait_recording(10)
    camera.stop_recording()
    camera.stop_preview()

    #copy from RAM device to NAS device
    shutil.copyfile(RAMname, NASname)

    #delete file from RAM device
    os.unlink(RAMname)      #os.remove() is same function

    #set up next check time
    time.sleep((triggerTime + 30.0) - time.time())
-=-=-=-=-

    Obviously you'll have to replace "/path/to/NAS/mount/point" with the
correct specification for your system. A proper NAS should appear as just
part of the file system -- no use of FTP or other fancy protocols should be
needed to access it.

    


--  
    Wulfraed                 Dennis Lee Bieber         AF6VN
     snipped-for-privacy@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

ftp causing invalid syntax
 On 2018 May 15 17:38:48, you wrote to All:

 DB> {Obviously untested -- I don't have a picamera, and I don't have a NAS}
 DB> {Save as "CameraExample.py", run by enterings "python CameraExample.py" at
 DB> the command line}
 DB> {Watch out for line wrap on long comments; and don't mess up the
 DB> indentation}
 DB> -=-=-=-=-
     #!/usr/bin/env python3
 DB> from picamera import PiCamera
 DB> import time
 DB> import os
 DB> import os.path
 DB> import shutil

FTFY

)\/(ark

Always Mount a Scratch Monkey
Do you manage your own servers? If you are not running an IDS/IPS yer doin' it
wrong...
... Boiled chicken ovulations with crisped pig lard? Delicious!

Re: ftp causing invalid syntax
On 15/05/18 22:38, Dennis Lee Bieber wrote:
Quoted text here. Click to load it
Thanks for the above code, and when I run it , it returns this, when  
movement is detected,:

Traceback (most recent call last)
  File "intruder_recmotion.py",  line 37, in <module>
   shutil.copyfile(RAMname, NASname)
  File "/usr/lib/python 2.7/shutil.py", line 83, in copyfile with  
open(dst, 'wb') as fdst:
IOError: [Errno 2] No such file or directory:  
'/192.168.0.22/mnt/CCTV/PiZero/2018-16-16_11.25.36.h264'

I have checked the path to the PiZero folder or dataset on my FreeNAS  
box and it is correct.

Thanks again

Site Timeline