问题描述
I am using below code with flask and name is app1.py
app = Flask(__name__)
api = Api(app)
#Class to create fileshare
class FileShare(Resource):
def post(self):
# Get JSON arguments from Payload shared NAS path,directorname groupname with read access and right access
parentdir = request.json.get("shareUNCPath")
dirname = request.json.get("shareFolderName")
readGroup = request.json.get("readGroup")
writeGroup = request.json.get("writeGroup")
#Create shared path with UNC Path and sharefolder name
path = os.path.join(parentdir,dirname)
# Access the NAS path through NAS credentails
class Impersonate:
def __init__(self,user,password):
#Update domain to access the shared NAS
self.domain_name = "<domain>"
self.user = user
self.password = password
logging.debug("Credentials Received: {} ".format(self.user))
def logon(self):
self.handle=win32security.LogonUser(self.user,self.domain_name,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__":
#update username and password of the NAS path below within quotes
a=Impersonate('username','password')
try:
a.logon() #Logon to NAS path with supplied credentails.
try:
logging.debug("Sucessfully connectd to NAS path {} ".format(parentdir))
# makedirs create directory recursively
os.makedirs(path)
#Set Permissions for the share folder for respective group
#Get the value of the groups stored in groupr and groupw as variables
try:
groupr,domain,type = win32security.LookupAccountName ("",readGroup)
sd = win32security.GetFileSecurity(path,win32security.DACL_SECURITY_INFORMATION)
dacl = sd.GetSecurityDescriptorDacl()
#set permessions for readGroup with GENERIC_READ level permessions
dacl.AddAccessAllowedAce(win32security.ACL_REVISION,win32con.GENERIC_READ,groupr)
except win32security.error as e:
resp = Response('Sucessfully created fileshare {} however setting group permession for the group {} failed. Error {}'.format(dirname,readGroup,e))
print (resp)
resp.status_code = 301
return resp
sd.SetSecurityDescriptorDacl(1,dacl,0)
win32security.SetFileSecurity(path,win32security.DACL_SECURITY_INFORMATION,sd)
try:
groupw,writeGroup)
sd = win32security.GetFileSecurity(path,win32security.DACL_SECURITY_INFORMATION)
dacl = sd.GetSecurityDescriptorDacl()
#set permessions for writeGroup with GENERIC_WRITE level permessions
dacl.AddAccessAllowedAce(win32security.ACL_REVISION,win32con.GENERIC_WRITE,groupw)
except win32security.error as e:
resp = Response('Sucessfully created fileshare {} however setting group permession for the group {} failed. Error {}'.format(dirname,writeGroup,e))
print (resp)
resp.status_code = 301
return resp
sd.SetSecurityDescriptorDacl(1,0)
win32security.SetFileSecurity(path,sd)
dacl = sd.GetSecurityDescriptorDacl()
cnt=dacl.GetAceCount()
for i in range(0,cnt):
rev,access,usersid = dacl.GetAce(i)
user,group,type = win32security.LookupAccountSid('',usersid)
details = ('Group: {}/{}'.format(group,user),rev,access)
#return ("Success Fileshare created: {} ".format(dirname))
resp = Response('Successfully created file share {}. Details {}'.format(dirname,details))
print (resp)
resp.status_code = 200
return resp
except OSError as error:
print(error)
resp = Response('NAS operatoon failed,{} fileshare creation failed. Error - {}'.format(dirname,error))
print (resp)
resp.status_code = 300
return resp
#return ("Fileshare creation failed: {} ".format(dirname))
except Exception as error1:
print(error1)
logging.error("Failed to connect to NASa path{},Error: {} ".format(parentdir,error1))
resp = Response('NAS Operation failed,could not connect to UNC Shared path. Error{}'.format(error1))
print (resp)
resp.status_code = 201
return resp
a.logoff()
api.add_resource(Create,'/test')
if __name__ == "__main__":
app.run(port=5001,host="0.0.0.0",use_reloader=True)
我用下面的app1.py创建了waitress_server.py,在下面的代码中被调用。 只需执行app1.py即可正常工作,但是当与服务生尝试时,我看到通过邮递员发送的HTTP帖子被接收为NULL。
我正在尝试使用女服务生进行生产设置。
from waitress import serve
import app1
serve(app1.app,host='0.0.0.0',port=5001)
当我打电话给邮递员时,我看到响应为空。如果我做得对,请帮助我。
解决方法
如果没有给定的错误,您忘记了返回响应。
特别是这部分:
if __name__ == "__main__":
dacl.AddAccessAllowedAce(win32security. ACL_REVISION,win32con.GENERIC_READ,groupr) #os.makedirs(path)
缺少返回语句。邮递员调用返回None
,因为默认情况下函数返回None
。
您需要在上述代码后添加Response
,并在所有后备时间之后返回它。
PS:感谢您添加代码