首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用waitress服务调用flask代码

如何使用waitress服务调用flask代码
EN

Stack Overflow用户
提问于 2020-08-19 01:31:07
回答 1查看 92关注 0票数 0
代码语言:javascript
复制
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, domain, type = win32security.LookupAccountName ("", 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, dacl, 0)                    
                    win32security.SetFileSecurity(path, win32security.DACL_SECURITY_INFORMATION, 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)

我用下面的代码创建了waitress_server.py,app1.py在下面的代码中被调用。只需执行app1.py就可以了,但是当我尝试使用waitress时,我看到通过postman的HTTP post被接收为NULL。

我正在尝试使用waitress来设置我的生产环境。

代码语言:javascript
复制
from waitress import serve
import app1
serve(app1.app, host='0.0.0.0', port=5001)

当我打电话给邮递员时,我看到响应为空。如果我做得对,请帮帮我。

EN

回答 1

Stack Overflow用户

发布于 2020-08-20 01:07:09

在没有给定错误的情况下,您忘记了返回响应。

特别是这一部分:

代码语言:javascript
复制
if __name__ == "__main__":
    dacl.AddAccessAllowedAce(win32security.      ACL_REVISION,win32con.GENERIC_READ, groupr) #os.makedirs(path)

缺少返回语句。因为默认情况下函数返回None,所以postman调用返回None

您需要在上述代码之后添加一个Response,并在所有回退之后返回它。

PS:感谢您添加代码

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63473766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档