Python Cookbook 2Nd Edition Jun 1002005 [Electronic resources]

David Ascher, Alex Martelli, Anna Ravenscroft

نسخه متنی -صفحه : 394/ 221
نمايش فراداده

Recipe 10.14. Creating a Share on Windows

Credit: John Nielsen

Problem

You want to share a folder of your Windows PC on a LAN.

Solution

PyWin32's win32net module makes this task very easy:

import win32net
import win32netcon
shinfo={  }
shinfo['netname'] = 'python test'
shinfo['type'] = win32netcon.STYPE_DISKTREE
shinfo['remark'] = 'data files'
shinfo['permissions'] = 0
shinfo['max_uses'] = -1
shinfo['current_uses'] = 0
shinfo['path'] = 'c:\\my_data'
shinfo['passwd'] = ''
server = 'servername'
win32net.NetShareAdd(server, 2, shinfo)

Discussion

While the task of sharing a folder is indeed fairly easy to accomplish, finding the information on how you do so isn't. All I could find in the win32net documentation was that you needed to pass a dictionary holding the share's data "in the format of SHARE_INFO_*." I finally managed to integrate this tidbit with the details from the Windows SDK (http://msdn.microsoft.com) and produce the information in this recipe. One detail that took me some effort to discover is that the constants you need to use as the value for the 'type' enTRy are "hidden away" in the win32netcon module.

See Also

PyWin32 docs at http://sourceforge.net/projects/pywin32/; Microsoft's MSDN site, http://msdn.microsoft.com.