我试图使用https://www.safaribooksonline.com/library/view/python-cookbook/0596001673/ch07s14.html中的示例连接到网络上另一个帐户可以访问的数据库。我得到的错误是:
r'DSN=测试'
LookupError:未知编码:utf-16le
import win32security, win32con
class Impersonate:
def _ _init_ _(self, login, password):
self.domain = 'bedrock'
self.login = login
self.password = password
def logon(self):
self.handle = win32security.LogonUser(self.login, self.domain,
self.password, win32con.LOGON32_LOGON_INTERACTIVE,
win32con.LOGON32_PROVIDER_DEFAULT)
win32security.ImpersonateLoggedOnUser(self.handle)
def logoff(self):
win32security.RevertToSelf( ) # terminates impersonation
self.handle.Close( ) # guarantees cleanup
if __name__=='__main__':
a = Impersonate('barney', 'bambam')
try:
a.logon() # become the user
try:
# Do whatever you need to do, e.g.,:
print win32api.GetUserName() # show you're someone else
cnxn = pyodbc.connect(
r'DSN=Test;'
)
finally:
a.logoff() # Ensure return-to-normal no matter what
except:
print 'Exception:', sys.exc_type, sys.exc_value
多亏了帕特里克,文件帮助了我,我的解决方案是:
相关问题 更多 >
编程相关推荐