Hi again,
here is a slightly improved version of twserver.py (moved timestamp before
.html in backup file names, tidied code).
#!/usr/bin/python3
import datetime, shutil, os, http.server
def makebackup(src):
(srcpath, srcfile) = os.path.split(src)
(srcname, src_ext) = os.path.splitext(srcfile)
tstamp=datetime.datetime.now().strftime("%Y%m%d%H%M%S")
dstpath = os.path.join(srcpath,'twBackups')
if not os.path.exists(dstpath):
os.mkdir(dstpath)
shutil.copyfile(src, os.path.join(dstpath, srcname+'-'+tstamp+src_ext))
class ExtendedHandler(http.server.SimpleHTTPRequestHandler):
def do_OPTIONS(self):
self.send_response(200, 'OK')
self.send_header('allow','GET,HEAD,POST,OPTIONS,CONNECT,PUT,DAV,dav'
)
self.send_header('x-api-access-type','file')
self.send_header('dav','tw5/put')
self.end_headers()
def do_PUT(self):
path = self.translate_path(self.path)
makebackup(path)
with open(path, "wb") as dst:
dst.write(self.rfile.read(int(self.headers['Content-Length'])))
self.send_response(200, 'OK')
self.end_headers()
os.chdir(os.path.dirname(os.path.abspath(__file__)))
http.server.HTTPServer(('localhost',8080),ExtendedHandler).serve_forever()
Like it's template,
https://tiddlywiki.com/#Saving%20via%20a%20Minimal%20Ruby%20Server, it
covers only the bare minimum TW server needs.
Feel free to use it / customise it at will.
Am Samstag, 28. Dezember 2019 17:52:43 UTC+1 schrieb UBi:
>
> Hello,
>
> I had some spare time recently, so I tried to re-implement the Ruby Server
> in Python:
>
> #!/usr/bin/python3
>
> from http.server import SimpleHTTPRequestHandler, HTTPServer
> import datetime, shutil, os
>
> def makebackup(src):
> (srcpath, srcfile) = os.path.split(src)
> tstamp=datetime.datetime.now().strftime("%Y%m%d%H%M%S")
> dstpath = os.path.join(srcpath,'twBackups')
> if not os.path.exists(dstpath):
> os.mkdir(dstpath)
> shutil.copyfile(src, os.path.join(dstpath, srcfile+'.'+tstamp))
>
> class ExtendedHandler(SimpleHTTPRequestHandler):
> def do_OPTIONS(self):
> self.send_response(200, 'OK')
> self.send_header('allow',
> 'GET,HEAD,POST,OPTIONS,CONNECT,PUT,DAV,dav')
> self.send_header('x-api-access-type','file')
> self.send_header('dav','tw5/put')
> self.end_headers()
> def do_PUT(self):
> length = int(self.headers['Content-Length'])
> path = self.translate_path(self.path)
> makebackup(path)
> with open(path, "wb") as dst:
> dst.write(self.rfile.read(length))
> self.send_response(200, 'OK')
> self.send_header('Content-Type', 'text/html')
> self.end_headers()
>
> os.chdir(os.path.dirname(os.path.abspath(__file__)))
> HTTPServer(('localhost',8080),ExtendedHandler).serve_forever()
>
> It works for me :-)
>
>
--
You received this message because you are subscribed to the Google Groups
"TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/tiddlywiki/80ca81b0-61b1-46c0-b35f-b6e15dfa8d69%40googlegroups.com.