n:
#!/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 :-)
/usr/bin/python3 ~/TW/twserver.py
--
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 tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/1a748dec-a911-478e-ba49-647c0c6a4e5b%40googlegroups.com.
Mohammad@6600K C:\TW\201. Python server $ python server.py 127.0.0.1 - - [29/Dec/2019 09:14:54] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [29/Dec/2019 09:14:54] code 404, message File not found 127.0.0.1 - - [29/Dec/2019 09:14:54] "GET /favicon.ico HTTP/1.1" 404 - 127.0.0.1 - - [29/Dec/2019 09:15:37] "GET /wikis/ HTTP/1.1" 200 - 127.0.0.1 - - [29/Dec/2019 09:15:40] "GET /wikis/tw.html HTTP/1.1" 200 - 127.0.0.1 - - [29/Dec/2019 09:15:41] "OPTIONS /wikis/tw.html HTTP/1.1" 200 - 127.0.0.1 - - [29/Dec/2019 09:15:42] code 404, message File not found 127.0.0.1 - - [29/Dec/2019 09:15:42] "GET /wikis/favicon.ico HTTP/1.1" 404 - 127.0.0.1 - - [29/Dec/2019 09:16:16] "PUT /wikis/tw.html HTTP/1.1" 200 - 127.0.0.1 - - [29/Dec/2019 09:16:16] "HEAD /wikis/tw.html HTTP/1.1" 200 - 127.0.0.1 - - [29/Dec/2019 09:41:05] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [29/Dec/2019 09:41:06] code 404, message File not found 127.0.0.1 - - [29/Dec/2019 09:41:06] "GET /favicon.ico HTTP/1.1" 404 -
#!/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()
Can we include a simple overview for people not familiar with python and its environment? I presume you can get it for multiple os implementations and make use of it on most platforms. Does it have defaults that may clash with other local hosts or specific ports etc... Basicaly we need to provide the information people need to select it without first installing it.
Regards
Tony
Hi all,I fiddled with the script a bit today for my personal use, and since this is a hot thread, I'm back to share my little improvements with you.I'm very new to tiddly so please advise if I should share these things somewhere else.
In the tiddlyserver script, I added a function to cleanup the backedup files. It keeps the last X files (Default=5) and is triggered right after a backup is made.I also added https functionality.
Please note that I also made a slight change in the folder structure, I keep things at /home/user/tiddlyserver/. In there, we have the twBackups folder,the tiddlyserver.py script, and a /served folder which contains the .html files to be served. The motivation was to not serve the script itself, or the backups.
As there is a crowd of Windows user, as I explained above the backup folder can be set by user in a simple way!
There is an installer utility (maybe more than one) that will wrap your script into it's own windows executable. The thing is, it won't be certified, so may ring alarm bells. In that sense, it may be easier to install python which (I assume) will have been certified.
Hey Mohammad,> Is it possible on server start launch a wiki to act as the landing page and from there I could click any wiki I like to openIf you have a wiki named "index.html" in the served folder, the python server will automatically launch that instead of the directory view.The downside is that as far as I know, you cannot then revert to the directory view so you will have to manually go to any other wiki.You would then have to maintain links to your other wikis in your main wiki. I'll add a note of this in the repo.> How serve folders located on different drive for example C:/TW, D:/myNotes, ...
Hmmmm, I could change the served folder to be a parameter in the configuration file instead of defaulting to ./served, this way the server could easily serve any single folder.I'd have to also make sure that git is initialized in gitless folders but that shouldn't be too hard.Serving multiple folders sounds a bit more challenging... I could symlink a bunch of folders into one, but then I'm not sure how to handle the git issue.With regards to windows support, I personally don't have a windows machine so I can't do too much trial and error. I'd be happy to package this once it's a bit more stable,but I think for now copy pasting the files and folder structure should be easier for everyone. (git is available on windows right? They would also need that for my version)
ps.You got my name right the first time, it's Panos :P
Sorry for that, I was writting from cell phone!
[Service]
User=USER
WorkingDirectory=/home/USER/tiddlywiki/
ExecStart=/usr/bin/python3 /home/USER/tiddlywiki/tiddlyserver.py
Restart=always
print("Tiddlyserver started ...")theserver.serve_forever()
Mohammad@N550JK C:\Temp
$ python tiddlyserver.py
Tiddlyserver started ...
Exception happened during processing of request from ('127.0.0.1', 56450)
Traceback (most recent call last):
File "D:\Python\Python37\lib\socketserver.py", line 316, in _handle_request_noblock
self.process_request(request, client_address)
File "D:\Python\Python37\lib\socketserver.py", line 347, in process_request
self.finish_request(request, client_address)
File "D:\Python\Python37\lib\socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "D:\Python\Python37\lib\http\server.py", line 646, in __init__
super().__init__(*args, **kwargs)
File "D:\Python\Python37\lib\socketserver.py", line 720, in __init__
self.handle()
File "D:\Python\Python37\lib\http\server.py", line 426, in handle
self.handle_one_request()
File "D:\Python\Python37\lib\http\server.py", line 414, in handle_one_request
method()
File "tiddlyserver.py", line 91, in do_PUT
makebackup(path)
File "tiddlyserver.py", line 73, in makebackup
if int(self.args.VOCAL) >0:
NameError: name 'self' is not defined
$ python tiddlyserver.py127.0.0.1 - - [14/Jan/2020 21:50:11] "GET /empty.html HTTP/1.1" 304 -127.0.0.1 - - [14/Jan/2020 21:50:11] "OPTIONS /empty.html HTTP/1.1" 200 -----------------------------------------Exception happened during processing of request from ('127.0.0.1', 49777)
Traceback (most recent call last):
File "C:\Users\Mohammad\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 316, in _handle_request_noblock self.process_request(request, client_address) File "C:\Users\Mohammad\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 347, in process_request self.finish_request(request, client_address) File "C:\Users\Mohammad\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 360, in finish_request self.RequestHandlerClass(request, client_address, self) File "C:\Users\Mohammad\AppData\Local\Programs\Python\Python37\lib\http\server.py", line 646, in __init__ super().__init__(*args, **kwargs) File "C:\Users\Mohammad\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 720, in __init__ self.handle() File "C:\Users\Mohammad\AppData\Local\Programs\Python\Python37\lib\http\server.py", line 426, in handle self.handle_one_request() File "C:\Users\Mohammad\AppData\Local\Programs\Python\Python37\lib\http\server.py", line 414, in handle_one_request
method() File "tiddlyserver.py", line 91, in do_PUT makebackup(path) File "tiddlyserver.py", line 73, in makebackup
if int(args.VOCAL) >0:NameError: name 'args' is not defined
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 tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/8f03ef81-c7cc-49fc-b5d3-99dfb7bffe41n%40googlegroups.com.
You received this message because you are subscribed to a topic in the Google Groups "TiddlyWiki" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/tiddlywiki/M64suMWXDYQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/CAAV1gMAjwmuSSEBcbQYFoZBWbjHQnrhziSha8TqtEoXvDHJrQg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/CAHXc%3DbHNUbRX_K-8BU_jBAyEBQu0ixFT4w8oBV2e7FW0A8PPYA%40mail.gmail.com.