Open Windows shared folder through linux machine
Asked Answered
V

4

5

I am using python 2.5 on Ubuntu, and there's a machine in the same network called machine1. The folder is shared.

How to to get a file in a specific folder of that machine?

I have tried, with no success:

urllib.urlopen('\\machine1\folder\file.txt')
Vidovic answered 10/3, 2010 at 19:38 Comment(0)
B
6

Linux has a utiliy called smbmount, which can be found in package smbutils I believe.

This is a command line utility which mounts a Windows share to a directory on the local machine, optionally with username/password.

smbmount is I believe a utility which runs as root, so whether it's suitable for you I don't know. Maybe it can be used as user.

You could either mount the share by default on the Linux machine, thereby accessing the files on it as local files, or you could do the smbmount / smbumount from within the python script with exec or something like that.

mkdir WindowsShare # Do this only once
smbmount \\server\share /home/me/WindowsShare -ousername=...,password=...
ls /home/me/WindowsShare
smbumount /home/me/WindowsShare

Username and password can be written in a file for some security. Check the man page.

If you need something totally python have a look at pysmb. Terms to google for are python, smb, CIFS.

Benedict answered 10/3, 2010 at 20:5 Comment(2)
Thanks, I had this solution at hand. But I want to use it in last case. But thanks anyway. In the last case, I'll install a web service on windows (IIS). :(Vidovic
There's also pypi.python.org/pypi/PySmbClient which uses smbclient underneath (unlike pysmb which tries to re-implement SMB/CIFS).Datary
E
2

urllib does not understand the SMB protocol. You will need to use gio via pygobject in order to retrieve the file.

Ensample answered 10/3, 2010 at 19:56 Comment(0)
B
0

If the folder is shared, I think it should be mounted in ~/.gvfs. Perhaps you can simply use open on the path as you see it in ~/.gvfs.

Bascomb answered 10/3, 2010 at 20:5 Comment(1)
If gvfs-fuse-daemon is running and if the share has already been mounted in the current session. Which you could ensure, by checking that ~/.gvfs is mounted and running gvfs-mount, but why not just use GIO directly?Datary
W
-1

You should look for the default file browser.

And then you can execute the process and pass in the folder you want as an argument (smb://machine1/folder/).

For example on windows you would do:

execl("explorer.exe", "D:")

Try to look for the path to your file browser (most of the time it's Nautilus).

So:

execl("/bin/nautilus", "smb://.../")

Some info on execl

Weigle answered 10/3, 2010 at 19:44 Comment(2)
Ok, but... I think I didn't explain myself very clear. My python script is running on Ubuntu and I want just to get a file. And there's no services in this window machine.Vidovic
Please update your question with the update from your part. You want to open a file in particular?Weigle

© 2022 - 2024 — McMap. All rights reserved.