0

I am calling python function to call a script to create a directory.

app = Flask(__name__)

 

@app.route('/', methods=['POST'])

def share_drive():

    try:
    
        parentdir = request.json.get("parentdir")

        dirname = request.json.get("dirname")

        #parentdir = request.values.get("parentdir")

        #dirname = request.values.get("dirname")

                path = os.path.join(parentdir, dirname)

    # makedirs create directory recursively

    try:
    
        os.makedirs(path)

        #return ("Success Fileshare created: {} ".format(dirname))
        resp = make_resopnse('{} successfully created.'.format(dirname))
        resp.status_code = 200
        return resp
    
    except OSError as error:
    
        #return ("Fileshare creation failed: {} ".format(dirname))       
        resp = make_resopnse('Failed to create fileshare {}'.format(dirname))
        resp.status_code = 400
        return resp 

I am calling it through post man, return statements are working. But I am making response from return and passing resp.status code and that part is failing.

Error in post man

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>
2
  • The code is not complete. At least the second except statement or finally statement is missing. It is possible that no OSError was raised but a different one, or as GAEfan suggest, another exception is raised in your except block. Commented Jul 24, 2020 at 14:36
  • If nothing helps, I recommend to use a debugger - see youtube.com/watch?v=Fxkco-gS4S8 Commented Jul 24, 2020 at 14:37

2 Answers 2

1

Because there are typos in your except:

return ("Fileshare creation failed: {} ". formart(dirname)) 

should be:

return ("Fileshare creation failed: {} ".format(dirname)) 

and, in case dirname is not defined (it should be always, but good coding form to show the exact error):

print(error)
return ("Fileshare creation failed: {} ".format(dirname)) 
Sign up to request clarification or add additional context in comments.

Comments

0
    os.makedirs(path)

    #return ("Success Fileshare created: {} ".format(dirname))
    resp = Response('{} successfully created.'.format(dirname))
    print (resp)
    resp.status_code = 200
    return resp

except OSError as error:

    #print(error)
    resp = Response('{} fileshare creation failed.{} filename  already exists'.format(dirname, error))
    print (resp)
    resp.status_code = 200
    return resp 
    #return ("Fileshare creation failed: {} ".format(dirname)) 

This has fixed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.