Ad

Errno 2 No Such File Or Directory: '/storage/test.txt'

- 1 answer

I am trying to create a Bottle server that allows the user to save a file to the server (I know this has a big security hole, adding an auth system later). When I try to save a file using the webpage, I get this error. [Errno 2] No such file or directory: '/storage/test.txt'

main.py

from bottle import *

class File():
  def __init__(self, Path):
    self.Path = Path
  def read(self):
    self.File = open(self.Path, 'r')
    return [self.File.read(), self.File.close()][0]

@route('/')
def Request():
  return File('responses/index.html').read()

@route('/actions/save')
def Request():
  return File('responses/actions/save/index.html').read()

@route('/actions/save', method='POST')
def Request():
  Source =  request.forms.get('source')
  Path =    request.forms.get('path')
  try:
    FileObj =  open('/storage/{}'.format(Path), 'w')
    FileObj.write(Source)
    FileObj.close()
    return File('responses/actions/save/ok.html').read()
  except Exception as Message:
    return File('responses/actions/save/error.html').read().format(Message=Message)

if __name__ == '__main__':
  run(host='0.0.0.0', port=8000)

responses/actions/save/index.html

  <html>
    <head>
      <link rel="stylesheet" target="_blank" rel="nofollow noreferrer" href="/style/index.css">
      <title>Save File</title>
    </head>
    <body>
      <h1>Save File</h1>
      <form action="/actions/save" method="post">
        <textarea name="source" cols="40" rows="10"></textarea><br>
        Path: <input type="text" name="path" value="/public"><br>
        <input type="submit" value="Submit">
      </form>
    </body>
  </html>

index.html

<html>
    <head>
      <link rel="stylesheet" target="_blank" rel="nofollow noreferrer" href="/style/index.css">
      <title>POSTRun</title>
    </head>
    <body>
      <h1>PostRun</h1>
      <a href='/actions/save'>Save File</a>
    </body>
  </html>
Ad

Answer

Bottle is handling paths relative to the server root, e.g. @route('/actions/save'). But open is a python operator and is specifying a path on your system. Change open('/storage/{}'.format(Path), 'w') to the path to the storage files relative to your python interpreter (e.g. storage/{})

Ad
source: stackoverflow.com
Ad