Skip to content

Commit

Permalink
fixed issue pedosb#5, implemented issue pedosb#8
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
itelo committed Apr 27, 2015
1 parent 8a61d44 commit cf09359
Showing 1 changed file with 96 additions and 76 deletions.
172 changes: 96 additions & 76 deletions webnote.py
Original file line number Diff line number Diff line change
@@ -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 += """
<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:
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 += """
<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()

0 comments on commit cf09359

Please sign in to comment.