简体   繁体   English

在 python 脚本中访问共享的 smb ubuntu

[英]Accessing shared smb ubuntu in python scripts

I have a shared ubuntu drive on my network that I can access in nautilus using smb://servername/sharedfolder or smb:///sharedfolder.我的网络上有一个共享的 ubuntu 驱动器,我可以使用 smb://servername/sharedfolder 或 smb:///sharedfolder 在 nautilus 中访问它。

I need to be able to access it python scripting from my ubuntu machine (8.10), but I can't figure out how.我需要能够从我的 ubuntu 机器(8.10)访问它的 python 脚本,但我不知道如何。 I tried the obvious way (same address as nautilus), but wasn't successful.我尝试了明显的方法(与鹦鹉螺相同的地址),但没有成功。

To play around, I tried printing the content of that folder with both:为了玩耍,我尝试使用以下两种方式打印该文件夹的内容:

Code: for file in os.listdir("smb://servername/sharedfolder") print file Both give me "no file or directory" error on that path.代码:for file in os.listdir("smb://servername/sharedfolder") print file 在该路径上都给我“没有文件或目录”错误。

I'd really appreciate help on this - thanks.我真的很感激这方面的帮助 - 谢谢。

Python can only handle local paths. Python 只能处理本地路径。 Samba is a remote path read by a driver or application in your Linux system and can there for not be directly accessed from Python unless you're using a custom library like this experimental library . Samba 是您的 Linux 系统中的驱动程序或应用程序读取的远程路径,除非您使用像这个实验库这样的自定义库,否则不能直接从 Python 访问。

You could do something similar to (make sure your user has the permission needed to mount stuff):您可以执行类似的操作(确保您的用户具有挂载所需的权限):

import os
from subprocess import Popen, PIPE, STDOUT

# Note: Try with shell=True should not be used, but it increases readability to new users from my years of teaching people Python.
process = Popen('mkdir ~/mnt && mount -t cifs //myserver_ip_address/myshare ~/mnt -o username=samb_user,noexec', shell=True, stdout=PIPE, stderr=PIPE)
while process.poll() is None:
    print(process.stdout.readline()) # For debugging purposes, in case it asks for password or anything.

print(os.listdir('~/mnt'))

Again, using shell=True is dangerous, it should be False and you should pass the command-string as a list.同样,使用shell=True是危险的,它应该是False并且您应该将命令字符串作为列表传递。 But for some reason it appears "complex" if you use it the way you're supposed to, so i'll write you this warning and you can choose to follow common guidelines or just use this to try the functionality out.但是由于某种原因,如果你按照你应该的方式使用它,它会显得“复杂”,所以我会写给你这个警告,你可以选择遵循通用指南,或者只是使用它来尝试功能。

Here's a complete guide on how to manually mount samba .这是有关如何手动挂载 samba 的完整指南 Follow this, and replace the manual steps with automated programming.遵循这一点,并用自动编程代替手动步骤。

Python's file-handling functions like os.listdir don't take GNOME URLs like Nautilus does, they take filenames. Python 的文件处理函数(如os.listdir )不像 Nautilus 那样采用 GNOME URL,它们采用文件名。 Those aren't the same thing.这些不是一回事。

You have three basic options here:您在这里有三个基本选项:

  1. Ask GNOME to look up the URLs for you, the same way Nautilus does.让 GNOME 为您查找 URL,就像 Nautilus 一样。
  2. Pick a Python SMB client and use that instead.选择一个 Python SMB 客户端并使用它。 There are multiple options to choose from .多个选项可供选择
  3. Use an SMB filesystem plugin like CIFS VFS to mount the remote filesystem to a mount point so you can then access its files by pathname instead of by URL.使用CIFS VFS之类的 SMB 文件系统插件将远程文件系统挂载到挂载点,这样您就可以通过路径名而不是 URL 访问其文件。

I have mainly used the answer from "Torxed" with somes modifications:我主要使用了“Torxed”的答案并进行了一些修改:

import os, time
from subprocess import Popen, PIPE, STDOUT

process = Popen('sudo mount -t cifs //192.168.1.1/Raspi /home/pi/mnt -o vers=1.0,username=pi,password=YOUR_PASSWORD', shell=True, stdout=PIPE, stderr=PIPE)

while process.poll() is None:
    print(process.stdout.readline()) # For debugging purposes, 

# the following command will list the content of the folder 
# thus be sure to have files or folders in the shared folder to check 
# for a correct output

print(os.listdir('/home/pi/mnt'))

time.sleep(2)
process = Popen('sudo umount //192.168.1.1/Raspi', shell=True)
  1. I had to add "vers=1.0" before username (dont forget the comma)我必须在用户名之前添加“vers=1.0”(不要忘记逗号)
  2. I also directly add my password, thus i avoid any prompt.我也直接添加我的密码,因此我避免任何提示。
  3. if one is not sure about "which user? Pi user, Samba user?"如果不确定“哪个用户?Pi 用户,Samba 用户?” the answer is just the linux user (Pi, for me, works well but dont forget the vers=1.0 because it's required (I don't know why)答案只是 linux 用户(Pi,对我来说,效果很好,但不要忘记 vers=1.0,因为它是必需的(我不知道为什么)
  4. the "while" and the "sleep" may add a delay avoiding errors. “while”和“sleep”可能会增加延迟以避免错误。
  5. I also finish the script with the umont option to corretly close.我还使用 umont 选项完成脚本以正确关闭。 Because if you dont add this command in the python script, you can't launch/run it twice.因为如果你不在 python 脚本中添加这个命令,你就不能启动/运行它两次。 You need to launch the umount command in the terminal in order to be able to remount it.您需要在终端中启动 umount 命令才能重新安装它。 Thus, it may be more convenient to show it directly in the python script.因此,直接在 python 脚本中显示它可能更方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM