I have a remote (Windows) server I'd like to connect to, and process some folder.
I tried to use SMBHandler unsuccessfully:
director = urllib2.build_opener(SMBHandler)
fh = director.open('smb://myuserID:mypassword#192.168.1.1/Publish')
It failed with the following error:
URLError: <urlopen error smb error: Failed to retrieve on Publish: Unable to open file
I wrote the following code that works perfectly using SMBConnection but I'm sure there's a better way to fetch a folder and query its subfolders and files...
conn = SMBConnection(USERID, PASSWORD, SERVER_NAME, SERVER_NAME, use_ntlm_v2 = True)
conn.connect(SERVER_IP, 139)
filelist = conn.listPath('Publish', '/')
if filelist[5].isDirectory:
print filelist[5].filename
etc...
Can you please help me finding the most elegant way to solve my problem?
Thank you very much :)
Nili
pysmb's SMBHandler does not allow you to list the files in the folder via urllib.
As of now, what you have done using SMBConnection is the recommended way to list the files in the folder.
Related
I have a web scraping script that is otherwise 100% functional. It is pointed at a website, stores attributes, paginates, and should be storing images for ConvNet processing, but I get an "PermissionError: [Errno 13] Permission denied [directory]" - when I comment out the image storage portion of the script, everything else works great.
I've tried changing from open(directory, 'wb') to urllib.request.urlopne(url) as response, open(directory,' 'wb') but of course the permission issue isn't solved.
I've run the program in CMD as admin, I've run the program directly through Python IDLE as admin, I've given permission to myself (the run as user) as well as NETWORK SERVICES and LOCAL SERVICE to the root of the storage drive, but I still receive a permission denied error.
Some unique behavior that I've observed; it creates a folder where an image should be stored, AND throws a permission denied error. The level at which the folder is created is where the image should be. The folder takes the name of the image. To eliminate variables, I tried feeding in a static string for the file name, and I still get permission denied. The folder is named "EFake.jpg", for example, when that folder should be a retrieved image in .jpg format. I've searched far and wide for a resolution to this issue, I feel like I've tried everything, apologies if I'm missing the obvious.
The entry point:
if items.get('reference_number'):
download_img_to_disk(brand, listing_url_retrieve_id, items['entry_logo'], items.get('reference_number'))
else:
download_img_to_disk(brand, listing_url_retrieve_id, items['entry_logo'])
#The function:
def download_img_to_disk(brand, name, url, reference_number='uncatogrized'):
resp = requests.get(url)
directory = "Z:\Storage\\"+str(brand)+"\\"+str(reference_number)+"\\"+name+'.jpg'
if not os.path.exists(directory):
os.makedirs(directory)
with urllib.request.urlopen(url) as response, open(directory, 'wb') as out_file:
data = response.read()
out_file.write(data)
return "File Downloaded.."
I expect "Z:\Storage\"+str(brand)+"\"+str(reference_number)+"\"+name+'.jpg' to be an actual .jpg stored, but it's a folder named name+'.jpg' and I receive the "PermissionError: [Errno 13] Permission denied [directory]" error message simultaneously. I feel like the datatype is wrong, the level at which the .jpg should be stored is wrong... I just can't figure it out.
Using Python 3.6
successfully retrieving data from mssqlserver and querying oracle for historical data and exporting it via Pandas to a tab delimited file which I then want to be able to ftp to another mssql server location. Since I have not tried this before, I experimented as follows:
\\\\\\\\\\\\\\\\\\\
This works: passing hardcoded variable
\\\\\\\\\\\\\\\\\\\\
from ftplib import FTP
host='myserver'
host_name='group_user_name'
host_pw='hostpassword'
host_dir='myworkingdirectory'
ftp = FTP(host)
ftp.login(user=host_name, passwd =host_pw)
ftp.cwd(host_dir)
filename = 'c:/temp/cigfun.txt'
fn = 'cigfun.txt'
def placeFile():
ftp.storbinary('STOR '+fn, open(filename, 'rb'))
ftp.quit()
placeFile()
\\\\\\\\\\\\\\
This also works: hard coded
\\\\\\\\\\\\\\
from ftplib import FTP
ftp = FTP('myserver')
ftp.login(user='group_user_name', passwd ='hostpassword')
ftp.cwd('myworkingdirectory')
ftp.retrlines('LIST')
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
This does not connect: retrieving data from stored procedure in MSSQL
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
if sql_id == str(5):
sql_cursor.execute("[dbo].[usp_GetProcess_"+seq_action+"] #sql_id = "+ sql_id)
table = sql_cursor.fetchall()
for row in table:
host = "'"+row[1]+"'"
host_name = "'"+row[2]+"'"
host_pw = "'"+row[3]+"'"
host_dir = "'"+row[4]+"'"
host_dir = re.sub('[cd ]', '', host_dir)
ftp_file = row[9]
f_type = row[10]
if f_type == "Tab (txt)":
f_type = '.txt'
elif f_type == "Comma (csv)":
f_type = '.csv'
else: f_type = '.xlsx'
path_file = "'"+ftp_file + f_type+"'"
fn="'"+os.path.basename(path_file)
path_file = path_file.replace(r'\\','/')
from ftplib import FTP
ftp = FTP(host)
ftp.login(user = host_name, passwd = host_pw)
ftp.cwd(host_dir)
\\\\\\\\\\\\\\
throws the following error:
File "C:\Users\A22021\AppData\Local\Continuum\anaconda332\lib\socket.py", line 745, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
gaierror: [Errno 11004] getaddrinfo failed
aside from my awkward efforts at coding (only my 3rd production attempt using Python), can you see anything obvious that would cause the failure? error messages are so cryptic.
I suspect it might have something to do with coded variable assignments, but I am baffled right now. Any constructive advice appreciated.
PLG
If everything works when you hard-code the FTP information, then the issue here is most likely the way you are parsing the query results. You need to see exactly what is getting put into your host* variables. To start, you could just pepper your code with some print statements, but I would highly suggest using the python debugger instead. This probably sounds scary, but it's not that bad. Run it like this:
python -m pdb my_script.py
You can then step through each line of your code by pressing n. So for example, when you hit the line that says for row in table: and you press n you will now be inside that loop and can print(row). This will give you some insight into what is going wrong with the parsing.
I have the following code, and I run it from localhost:
def create_names_file(req, names, data, profileid):
s = names
fname = str(profileid)
fpath = req.conf["inf_path"]+"/"+fname
f = open(fpath, 'w')
req.conf["inf_path"] is /opt/fp/trunk/analysis/2/, and I receive permission error.I use Ubuntu OS. How can I solve this problem?
You seem to be trying to open a file named /opt/fp/trunk/analysis/2/ which in invalid due to the trailing slash. Possibly that is a typo so, if the required file already exists, who owns it?
Does the user that you run Python as have permissions to write to that file?
Check the permissions reported by ls -l /opt/fp/trunk/analysis/2.
I have a Odoo8 running on my linux server and I need to copy a file from this server to a Windows 10 shared folder with authentication.
I tried to do it programmatically like this:
full_path = "smb://hostname/shared_folder/other_path"
if not os.path.exists(full_path):
os.makedirs(full_path)
full_path = os.path.join(full_path, file_name)
bin_value = stream.decode('base64')
if not os.path.exists(full_path):
try:
with open(full_path, 'wb') as fp:
fp.write(bin_value)
fp.close()
return True
except IOError:
_logger.exception("stream_save writing %s", full_path)
but even if no exception is raised, folders are not created and file is not written.
Then I tried to remove the "smb:" part from the uri and it raised an exception regarding authentication.
I'd like to fix the problem just by using python, possibly avoiding os.system calls or external scripts, but if no other way is possible, then any suggestion is welcome.
I also tried with
"//user:password#hostname"
and
"//domain;user:password#hostname"
both with and without smb
Well, I found it out by myself a way using SAMBA:
First you need to install pysmb (pip install pysmb) then:
from smb.SMBConnection import SMBConnection
conn = SMBConnection(user, password, "my_name", server, domain=domain, use_ntlm_v2 = True)
conn.connect(ip_server)
conn.createDirectory(shared_folder, sub_directory)
file_obj = open(local_path_file,'rb')
conn.storeFile(shared_folder, sub_directory+"/"+filename, file_obj)
file_obj.close()
in my case sub_directory is a whole path, thus I need to create each folder one by one (createDirectory works only this way) and everytime I need to check if the directory does not already exists because otherwise createDirectory raise an exception.
I hope my solution could be useful for others.
If anybody find a better solution, please answer...
I'm attempting to make the switch from Windows to ubuntu (am using 12.04 LTS) and am trying to use some of my old scripts to run my old databases.
Previously I used postgresql and psycopg2 to maintain them and I am trying to do so again here.
My error is around importing a csv file to a table using the copy expert command.
Code is as follows:
#!/usr/bin/env python
import psycopg2 as psy
import sys
conn = psy.connect("dbname, user, host, password") # with the appropriate values
curs = conn.cursor()
table = 'tablename' # a table with the appropriate columns etc
file = 'filename' # a csv file
SQL = "COPY %s FROM '%s' WITH CSV HEADERS" % (tablename, filename)
curs.copy_expert(SQL, sys.stdin) # Error occurs here
conn.commit()
curs.close()
conn.close()
The specific error which is occurring is as follows:
psycopg2.ProgrammingError: could not open file "filename" for reading: Permission denied
Any assistance would be greatly appreciated:
I am completely stuck and I believe it is due some quirk of how I've set up the database or the files.
Adding a simple read and print command using the csv module works fine as well (from the same script in fact) It will output all of the information from the csv file and then error out with the permission denied when attempting to import it to the database
import csv
f = open(filename, 'rb')
read = csv.reader(f, delimiter =',')
for row in read:
print row
f.close()
Try executing the command as the super user by using su or sudo and if this doesn't help, the other possiblity is that the location of the filename is out of bounds so I would try copying it to the desktop or your home directory or folder where you know you definitely have full permissions and see if this works.