forked from pedosb/note
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Caso nao tenha o arquivo .notes, o webnotes.py se encarrega de cria-lo. Foi adicionado o suporte para criacao de notas pelo webnotes.py.
- Loading branch information
Showing
1 changed file
with
89 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,108 @@ | ||
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', 8080)) | ||
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) | ||
else: | ||
raise Exception('Cannot read request header') | ||
|
||
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 += """ | ||
<html> | ||
<head> | ||
<meta http-equiv="content-type" content="text/html; charset=utf-8"/> | ||
</head> | ||
<body> | ||
<form action="add_note" method="POST"> | ||
Name: <input type="text" name="name"/> | ||
<br/> | ||
Content: <input type="text" name="content"/> | ||
<br/> | ||
<input type="submit" value="Add"/> | ||
</form> | ||
</body> | ||
</html> | ||
""" | ||
elif method == 'POST': | ||
while True: | ||
if len(data) != request_headers['Content-Lenght']: | ||
data += conn.recv(1024) | ||
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 += """ | ||
<html> | ||
<head> | ||
<meta http-equiv="content-type" content="text/h | ||
tml; charset=utf-8"/> | ||
</head> | ||
<body> | ||
<form action="add_note" method="POST"> | ||
Name: <input type="text" name="name"/> | ||
<br/> | ||
Content: <input type="text" name="content"/> | ||
<br/> | ||
<input type="submit" value="Add"/> | ||
</form> | ||
</body> | ||
</html> | ||
""" | ||
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() |