简体   繁体   中英

Accessing a Windows CIFS share from Python code running in a Docker container

I'm trying to access multiple Windows CIFS shares from a Python code that will run in a Docker container. I've seen that there are multiple SMB libraries like pysmb and smbprotocol that claim that they can be used to access CIFS shares, but I haven't managed to get it to work and haven't seen a single example online where they are used to access CIFS shares.

I know that a solution would be to mount the share on the host and mount it to the container, but I'd rather to avoid that if possible, as the code will need to access multiple shares and not all will be known when the container starts.

Am I missing something? Is there a good way or a good example online on how to access CIFS share from Python code that runs on Linux? (I know that on Windows you can simply open the folder, but I need it to work on Linux).

What is known to not work is a call to mount inside the container unless the container was started with privileges . However client code could also connect to CIFS drives without first mounting the directory (eg for Java it is jcifs-ng ).

Find out how the library you use works internally. If it can connect directly go on. Otherwise you may also add smbclient to your container and call that to access files on the CIFS side.

There's no CIFS specific example because it should work the same as SMB in linux. I have been struggling for a while with both libraries ( pysmb and smbprotocol ) and I have made them work.

For pysmb , the only thing I've found different is that examples usually tell you can leave domain empty or don't tell it. On windows, it is the workgroup. If leaving it empty doesn't work, you can check it out by right-clicking on “My Computer” and selecting “Properties”. You'll find it there together with the machine name.

Here's a working example with pysmb from linux to a windows machine:

from smb.SMBConnection import SMBConnection
conn = SMBConnection(username="user", password="my_passwrd", my_name="name", remote_name="WINDOWS-MACHINE", domain="WORKGROUP", use_ntlm_v2=True)
conn.connect(my_server_ip_addr)

for file in conn.listPath("shared_dir", "/"):
   print(file.filename)
conn.close()

You can check an example using smbprotocol here (where "server" is you IP address or host and "share" your shared directory name). You can make it work without remote_name nor domain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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