Skip to content

Instantly share code, notes, and snippets.

@nir9
Last active January 8, 2024 05:51
Show Gist options
  • Save nir9/f1ba0ab42a817f66fb74a4d002f000d0 to your computer and use it in GitHub Desktop.
Save nir9/f1ba0ab42a817f66fb74a4d002f000d0 to your computer and use it in GitHub Desktop.
Notes server in python
import socket
import datetime
"""
Instead of header+stuff
just make a function that adds that
"""
def list_to_html(l):
html = ""
for obj in l:
html = html + obj + "<br/>"
return html
HEADER = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"
notes = []
working = True
s = socket.socket()
s.bind(("", 80))
s.listen(1)
print "Running Server!"
while working:
client, addr = s.accept()
try:
data = client.recv(1024).split("\r\n")[0].split()[1]
except Exception:
continue
print "{:%Y-%m-%d %H:%M:%S}".format(datetime.datetime.now()),"req",data
if data == "/nir":
client.send(
HEADER + "Who is Nir?<br/>Nir is the mastermind behind this revolutionary notes website."
" so simple yet so amazing. taking notes and saving links has never been so easy.")
if data == "/show":
client.send(HEADER + list_to_html(notes))
if data == "/clear":
notes = []
client.send(HEADER + "Cleared!")
if data == "/help":
client.send(HEADER + "Commands: nir, show, clear, help, goto. Future ideas: Design, suggest ideas via website, dates,"
" save for timed and for long term.")
#if data == "/goto":
# client.send(HEADER + "<head><meta http-equiv=\"refresh\" content=\"0; url='" + notes[-1] + "'/></head>Redirecting.")
#if data == "/gallery":
else:
if "/?" in data[0:1]:
note = data.replace("/?", "")
note = note.replace("%20", " ")
client.send(
HEADER + "Got it thanks!<br/>Built and maintained by <a href=\"nir\">Nir</a>.<br/>Saved note " + note)
notes.append(note)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment