Python FTP won't Transfer - python

I don't know what I'm doing wrong but this small ftp code won't transfer files. I keep getting
File "example.py", line 11, in ?
ftp.storlines("STOR " + file, open(file))
ftplib.error_perm: 550 /home/helen/docs/example.txt: Operation not permitted
Here is the code:
import ftplib
file = '/home/helen/docs/example.txt'
ftp = ftplib.FTP('domain', 'user', 'password')
print "File List: "
files = ftp.dir()
ftp.cwd("/upload/")
ftp.storlines("STOR " + file, open(file))
f.close()
s.quit()
Any help would be appreciated.

I think the error you're getting is that you're adding the entire file path to the first argument in thestorlines()call. Instead, just specify the file name itself:
import os
ftp.storlines("STOR " + os.path.basename(file), open(file))
You might want to consider changingfiletofilepath,since that's what it really is (plus you will no longer be hiding the built-in function & type of the same name).

550 error literally means according to wikipedia "550 Requested action not taken. File unavailable (e.g., file not found, no access)."
http://en.wikipedia.org/wiki/List_of_FTP_server_return_codes
are you sure you have the right permissions?
try this
ftp = ftplib.FTP('domain')
ftp.login('user','pass')
i think the object creation was just a little jacked up.

Related

Python ftplib error 426 when putting files on iSeries

Have a peculiar issue that I can't seem to fix on my own..
I'm attempting to FTP a list of files in a directory over to an iSeries IFS using Python's ftplib library.
Note, the files are in a single subdirectory down from the python script.
Below is an excerpt of the code that is giving me trouble:
from ftplib import FTP
import os
localpath = os.getcwd() + '/Files/'
def putFiles():
hostname = 'host.name.com'
username = 'myuser'
password = 'mypassword'
myftp = FTP(hostname)
myftp.login(username, password)
myftp.cwd('/STUFF/HERE/')
for file in os.listdir(localpath):
if file.endswith('.csv'):
try:
file = localpath + file
print 'Attempting to move ' + file
myftp.storbinary("STOR " + file, open(file, 'rb'))
except Exception as e:
print(e)
The specific error that I am getting throw is:
Attempting to move /home/doug/Files/FILE.csv
426-Unable to open or create file /home/doug/Files to receive data.
426 Data transfer ended.
What I've done so far to troubleshoot:
Initially I thought this was a permissions issue on the directory containing my files. I used chmod 777 /home/doug/Files and re-ran my script, but the same exception occured.
Next I assumed there was an issue between my machine and the iSeries. I validated that I could indeed put files by using ftp. I was successfully able to put the file on the iSeries IFS using the shell FTP.
Thanks!
Solution
from ftplib import FTP
import os
localpath = os.getcwd() + '/Files/'
def putFiles():
hostname = 'host.name.com'
username = 'myuser'
password = 'mypassword'
myftp = FTP(hostname)
myftp.login(username, password)
myftp.cwd('/STUFF/HERE/')
for csv in os.listdir(localpath):
if csv.endswith('.csv'):
try:
myftp.storbinary("STOR " + csv, open(localpath + csv, 'rb'))
except Exception as e:
print(e)
As written, your code is trying to execute the following FTP command:
STOR /home/doug/Files/FILE.csv
Meaning it is trying to create /home/doug/Files/FILE.csv on the IFS. Is this what you want? I suspect that it isn't, given that you bothered to change the remote directory to /STUFF/HERE/.
If you are trying to issue the command
STOR FILE.csv
then you have to be careful how you deal with the Python variable that you've named file. In general, it's not recommended that you reassign a variable that is the target of a for loop, precisely because this type of confusion can occur. Choose a different variable name for localpath + file, and use that in your open(..., 'rb').
Incidentally, it looks like you're using Python 2, since there is a bare print statement with no parentheses. I'm sure you're aware that Python 3 is recommended by now, but if you do stick to Python 2, it's recommended that you avoid using file as a variable name, because it actually means something in Python 2 (it's the name of a type; specifically, the return type of the open function).

Python open says file doesn't exist when it does

I am trying to work out why my Python open call says a file doesn't exist when it does. If I enter the exact same file url in a browser the photo appears.
The error message I get is:
No such file or directory: 'https://yhistory.s3.amazonaws.com/media/userphotos/1_1471378042183_cdv_photo_033.jpg'
The Python code is:
full_path_filename = 'https://' + settings.AWS_STORAGE_BUCKET_NAME + '.s3.amazonaws.com/' + file_name
fd_img = open(full_path_filename, 'r')
I was wondering if this problem is something to do with the file being in an AWS S3 bucket but I am able to connect to the bucket and list its contents.
If you are trying to open a file over internet you should do something like this (assuming that you are using python 3):
import urllib.request
full_path_filename = 'https://' + settings.AWS_STORAGE_BUCKET_NAME + '.s3.amazonaws.com/' + file_name
file = urllib.request.urlopen(full_path_filename)
This will download the actual file. You can use file as an another file like object.

How to go backwards in an FTP connection : PYTHON

ftp.cwd("TXNnIGZvbGRlcg==/")
file = open('msg.txt', 'rb')
file.storbinary('STOR msg.txt', file)
file.close
So just a quick answer I need... the code shows a msg being saved in the base64 folder in the FTP server, however in earlier code, I've already said:
if name != "":
ftp.mkd(name)
ftp.cwd(name)
So it's already navigated somewhere, but I need help on finding the command on how to go back a directory.
something like
ftp.goback()
Or something.
I believe you can try something like this.
ftp.cwd("TXNnIGZvbGRlcg==/")
file = open('msg.txt', 'rb')
ftp.storbinary('STOR msg.txt', file)
file.close()
ftp.cwd("../")

Accesing a file in a ftp server

I have made a program, and there is a function where it gets a text file called news_2014.txt from a ftp server. I currently have this code:
def getnews():
server = 'my ftp server ip'
ftp= ftplib.FTP(server)
username = 'news2'
password = ' '
ftp.login(username,password)
filename = 'ftp://my ftp server ip/news/news_2014.txt'
path = 'news'
ftp.cwd(path)
ftp.retrlines('RETR' + filename, open(filename, "w").open)
I wanna make so the program displays the lines using readlines onto a Tkinter label. But if I try calling the top function, it says:
IOError: [Errno 22] invalid mode ('w') or filename: 'ftp://news/news_2014.txt'
RETR wants just the remote path name, not a URL. Similarly, you cannot open a URL; you need to pass it a valid local filename.
Changing it to filename = 'news_2014.txt' should fix this problem trivially.
The retrlines method retrieves the lines and optionally performs a callback. You have specified a callback to open a local file for writing, but that's hardly something you want to do for each retrieved line. Try this instead:
textlines = []
ftp.retrlines('RETR ' + filename, textlines.append)
then display the contents of textlines. (Notice the space between the RETR command and its argument, too.)
I would argue that the example in the documentation is confusing for a newcomer. Someone should file a bug report.

opening a pcap file in python

I'm trying to open a .pcap file in python. can anyone help with this? each time I try this, it gives an error message that "IOError: [Errno 2] No such file or directory: 'test.pcap'"
import dpkt
f = open('test.pcap')
pcap = dpkt.pcap.Reader(f)
Try giving open() the correct path to test.pcap:
f = open(r'C:\Users\hollandspur\Documents\test.pcap')
or some such...
As Tim pointed out above you probably need to use the entire file path because you're not in the same directory. If you're running from the interpreter you can check your path using the following:
import os
os.getcwd()
If you're not in the same directory as where you're file is stored then you need the full file path. You can type the whole thing in, or with a little more work you can accept relative file paths.
import os
relativePath = 'test.pcap' # Relative directory something like '../test.pcap'
fullPath = os.path.join(os.getcwd(),relativePath) # Produces something like '/home/hallandspur/Documents/test.pcap'
f = open(fullPath)
This would allow you to give a path such as "../test.pcap" which would go up one directory and look for the file. This is especially useful if you are running this script from the command line or your file is in a different directory that is close to the current directory.
You may also want to look into functions such as os.path.isfile(fullPath) that would let you check if the file exists
You should read as a binary file. See the 'rb' parameter which indicates to read this as binary file
import dpkt
f = open('test.pcap','rb')
pcap = dpkt.pcap.Reader(f)
I'm trying to open a .pcap file in python. can anyone help with this? each time I try this, it gives an error message that IOError: [Errno 2] No such file or directory: 'test.pcap'
Try this code: try this code to overcome the above ioerror
import dpkt,sys,os
"""
This program is open a pcap file and
count the number of packets present in it.
it also count the number of ip packet, tcp packets and udp packets.
......from irengbam tilokchan singh.
"""
counter=0
ipcounter=0
tcpcounter=0
udpcounter=0
filename=raw_input("Enter the pcap trace file:")
if os.path.isfile(filename):
print "Present: ",filename
trace=filename
else:
print "Absent: ",filename
sys.stderr.write("Cannot open file for reading\n")
sys.exit(-1)
for ts,pkt in dpkt.pcap.Reader(open(filen,'r')):
counter+=1
eth=dpkt.ethernet.Ethernet(pkt)
if eth.type!=dpkt.ethernet.ETH_TYPE_IP:
continue
ip=eth.data
ipcounter+=1
if ip.p==dpkt.ip.IP_PROTO_TCP: #ip.p == 6:
tcpcounter+=1
#tcp_analysis(ts,ip)
if ip.p==dpkt.ip.IP_PROTO_UDP: #ip.p==17:
udpcounter+=1
print "Total number of packets in the pcap file :", counter
print "Total number of ip packets :", ipcounter
print "Total number of tcp packets :", tcpcounter
print "Total number of udp packets :", udpcounter

Categories