Python operates Windows shared folders

Windows sharing

Shared folder

Select a folder at random, then right-click to grant access permissions -> Specific users
Insert image description here
to specify the permissions for the user to access. By default, the current user and administrator have read and write permissions. When accessing, you will be asked to enter the user name and password. You can also add Everyone, so that you do not need to specify a user password. The default permission for Everyone is readable and can be modified by yourself.
Insert image description here

shared disk

For example, to share the E drive, right-click the E drive on My Computer, select the sharing column->Advanced Sharing-check Sharing, and set permissions. If you don’t need Everyone’s access, you don’t need to modify the permissions.
Insert image description here

ubuntu mount

Assume that the shared computer IP is 192.168.1.2and the shared folder is Software
Create a folder for sharing
mkdir ~/Software
sudo mount -t cifs -o username=用户名,password=密码 //192.168.1.2/Software ~/Software

Note that ordinary users cannot use mount, so ~/Softwarethe permissions of the folder after mounting are only accessible to root.

python access

pip install pysmb

github: https://github.com/miketeo/pysmb
documentation: http://pysmb.readthedocs.io/

connect to the server
from smb.SMBConnection import SMBConnection

host = "192.168.1.2"  #ip或域名,改成你自己的
username = "user" #用户名,改成你自己的
password = "pass" #密码,改成你自己的
my_name = "aaaa" # 这个随便,可以为空字符串
remote_name = "WIN-1QI0CPE887P" # 这个是共享主机的主机名,listShares会用到,不用listShares的话可以为空字符串
conn = SMBConnection(username, password,my_name , remote_name , is_direct_tcp=True)
result = conn.connect(host, 445) #smb协议默认端口445
print("登录状态", result)

Error: ConnectionResetError: [Errno 104] Connection reset by peer
Just add is_direct_tcp=Trueit

List all directories currently shared by the server

The remote server host name, remote_name, needs to be given. Otherwise an exception will be thrown

for share in conn.listShares():
    print(share.name)
List all files in a shared directory

The first parameter is the name of the shared folder, which is the Software above. The second parameter is the path relative to the shared directory. For example, if you want to list Software/filethe directory, fill in the second parameter /file. The first one /should be optional.

for file in conn.listPath("Software", "/"):
    print(i.filename)
upload files

Upload the 1.txt file in the current directory to the server Software/11.txt. If it already exists, it will be overwritten.

with open('1.txt', 'rb') as f:
    conn.storeFile("Software","11.txt", f)
download file

Download the file from the server Software/11.txtto local and save it as 1.txt

with open('1.txt', 'wb') as fw:
    conn.retrieveFile("Software","11.txt",fw) 
Create folder

No return value. If no error is reported, the creation is successful.
conn.createDirectory("Software", "test")

Delete Files

The second parameter is the file to be deleted and can contain wildcard characters. The third parameter is whether to delete the directory
`conn.deleteFiles(“Software”, “test/*”, delete_matching_folders=True)

Get file information

Get file or folder information, such as creation time, file size, modification time, etc. Throws an exception if the file does not exist.

file_info = conn.getAttributes("Software", "11.txt")

# 更多属性可以dir(file_info )看下
print(file_info.create_time, file_info.alloc_size, file_info.file_size)
print(file_info.isDirectory, file_info.isReadOnly)
Rename file

conn.rename("Software"、"11.txt"、"22.txt")

Guess you like

Origin blog.csdn.net/Qwertyuiop2016/article/details/129367401