diff --git a/webnote.py b/webnote.py index a1f90fe..a00dea5 100644 --- a/webnote.py +++ b/webnote.py @@ -1,95 +1,115 @@ import socket import re import json +import urlparse +import urllib s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) -s.bind(('0.0.0.0', 80)) +s.bind(('0.0.0.0', 3000)) s.listen(1) -conn, addr = s.accept() -print 'Connected by', addr -data = '' - while True: - if '\r\n\r\n' in data: - break - new_data = conn.recv(1024) - if not new_data: - break - data += new_data + data = '' + conn, addr = s.accept() + print 'Connected by', addr + while True: + if '\r\n\r\n' in data: + break + new_data = conn.recv(1024) + if not new_data: + break + data += new_data -request_match = \ - re.match(r'^([^ ]+) ([^ ]+) ([^ ]+)\r\n(.*)$', - data, - re.DOTALL) + request_match = \ + re.match(r'^([^ ]+) ([^ ]+) ([^ ]+)\r\n(.*)$', data, re.DOTALL) + if request_match is not None: + method = request_match.group(1) + resource = request_match.group(2) + protocol = request_match.group(3) + data = request_match.group(4) + request_match = \ + re.match(r'^([^ ]+) ([^ ]+) ([^ ]+)\r\n(.*)$', data , re.DOTALL) + else: + raise Exception('Cannot read request') -if request_match is not None: - method = request_match.group(1) - resource = request_match.group(2) - protocol = request_match.group(3) - data = request_match.group(4) -else: - raise Exception('Cannot read request') + request_headers = dict() -request_headers = dict() -while True: - if data[:2] == '\r\n': - data = data[2:] - break - field = re.match(r'^([^:]+): ([^\r]+)\r\n(.*)$', data, re.DOTALL) - if field is not None: - field_name = field.group(1) - field_body = field.group(2) - request_headers[field_name] = field_body - data = field.group(3) - else: - raise Exception('Cannot read request header') + while True: + if data[:2] == '\r\n': + data = data[2:] + break + field = re.match(r'^([^:]+): ([^\r]+)\r\n(.*)$', data, re.DOTALL) + if field is not None: + field_name = field.group(1) + field_body = field.group(2) + request_headers[field_name] = field_body + data = field.group(3) + if field_name == "Content-Length": + body_len = int (field_body) + else: + raise Exception('Cannot read request header') + + print resource + print request_headers + + response = '' -print resource -print request_headers + response += 'HTTP/1.1 200 OK\r\n' -response = '' + try: + with open('.notes') as note_file: + note_dict = json.loads(note_file.read()) + except IOError: + note_dict = dict() -response += 'HTTP/1.1 200 OK\r\n' + if resource == '/notes': + response += 'Content-Type: text/plain; charset=utf-8\r\n' + response += '\r\n' + for name in note_dict: + response += '%s\n' % name + elif resource == '/add_note': + response += 'Content-Type: text/html; charset=utf-8\r\n' + response += '\r\n' + if method == 'GET': + response += """ + + + + + +
+ Name: +
+ Content: +
+ +
+ + + """ + elif method == 'POST': + while True: + rl_body_len = len(data) + if rl_body_len != body_len: + new_data = conn.recv(1024) + data += new_data + else: + break + new_dict = urlparse.parse_qs(data) + note_dict[new_dict['name'][0]] = new_dict['content'][0] + with open('.notes', 'w') as note_file: + json.dump(note_dict, note_file) -with open('.notes') as note_file: - note_dict = json.loads(note_file.read()) -if resource == '/notes': - response += 'Content-Type: text/plain; charset=utf-8\r\n' - response += '\r\n' - for name in note_dict: - response += '%s\n' % name -elif resource == '/add_note': - response += 'Content-Type: text/html; charset=utf-8\r\n' - response += '\r\n' - if method == 'GET': - response += """ - - - - - -
- Name: -
- Content: -
- -
- - - """ -else: - response += 'Content-Type: text/plain; charset=utf-8\r\n' - response += '\r\n' - note_match = re.match('^/notes/([a-z]+)$', resource) - if note_match is not None: - response += note_dict[note_match.group(1)] else: - response += 'Hello World!!!' + response += 'Content-Type: text/plain; charset=utf-8\r\n' + response += '\r\n' + note_match = re.match('^/notes/([a-z]+)$', resource) + if note_match is not None: + response += note_dict[note_match.group(1)] + else: + response += 'Hello World!!!' -conn.sendall(response) + conn.sendall(response) -conn.close() + conn.close()