Skip to content

Commit b98bb0d

Browse files
committed
initial
1 parent 2f2b4bf commit b98bb0d

File tree

10 files changed

+244
-0
lines changed

10 files changed

+244
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__

data/dns.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"name": "voting.com",
4+
"type": "A",
5+
"addr": [
6+
{"ip": "localhost", "port": 1010},
7+
{"ip": "localhost", "port": 1011}
8+
]
9+
}
10+
]

data/election.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"candidato 1": 0,
4+
"candidato 2": 0
5+
}
6+
]

data/keys.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

src/backup_server.py

Whitespace-only changes.

src/client.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from env import DNS_ADDRESS, DNS_PORT
2+
from utils import http_parser_reply
3+
4+
from socket import socket, AF_INET, SOCK_STREAM
5+
6+
def send_get_request(client_socket):
7+
msg = "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"
8+
client_socket.send(msg.encode())
9+
10+
def get_addr_from_name(name):
11+
client_socket = socket(AF_INET, SOCK_STREAM)
12+
client_socket.connect((DNS_ADDRESS, DNS_PORT))
13+
client_socket.send(name.encode())
14+
addr = client_socket.recv(1024).decode()
15+
client_socket.close()
16+
return addr
17+
18+
def print_menu(idx):
19+
menu_text = {
20+
1:
21+
'Bem vindo ao sistema de votação!\n' \
22+
'[0] Fechar programa\n' \
23+
'[1] Conectar-se ao servidor\n'
24+
,
25+
2:
26+
'Você tem certea que deseja sair? Você não poderá voltar a executar\n' \
27+
'[0] Não, voltar à tela anterior\n' \
28+
'[1] Sim, desejo encerrar a sessão\n'
29+
}
30+
31+
print(menu_text[idx])
32+
33+
34+
def menu():
35+
...
36+
37+
def main():
38+
addr = get_addr_from_name("voting.com").split(':')
39+
40+
client_socket = socket(AF_INET, SOCK_STREAM)
41+
client_socket.connect((addr[0], int(addr[1])))
42+
43+
i = 1
44+
while(i):
45+
send_get_request(client_socket)
46+
print("OK")
47+
# print(http_parser_reply(client_socket.recv(1024).decode()))
48+
i = input()
49+
50+
# while (1):
51+
# try:
52+
# msg = input()
53+
# except EOFError:
54+
# break
55+
56+
# client_socket = socket(AF_INET, SOCK_STREAM)
57+
# client_socket.connect(("127.0.0.1", 12345))
58+
59+
# client_socket.send(msg.encode())
60+
# msg = http_parser(client_socket.recv(1024).decode())
61+
# print(f"Mensagem recebida {msg}")
62+
63+
main()

src/dns.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from env import DNS_ADDRESS, DNS_PORT
2+
from utils import readjson
3+
4+
from socket import socket, AF_INET, SOCK_STREAM
5+
from threading import Thread
6+
7+
def handle_request(socket_client):
8+
request = socket_client.recv(2048).decode()
9+
10+
dns_client = socket(AF_INET, SOCK_STREAM)
11+
12+
for site in readjson():
13+
if site['name'] == request:
14+
for addr in site['addr']:
15+
try:
16+
dns_client.connect((addr['ip'], addr['port']))
17+
dns_client.close()
18+
socket_client.send(f"{addr['ip']}:{addr['port']}".encode())
19+
socket_client.close()
20+
except:
21+
print(f"{addr['ip']}:{addr['port']} is down.")
22+
continue
23+
24+
socket_client.close()
25+
return
26+
27+
def main():
28+
server_socket = socket(AF_INET, SOCK_STREAM)
29+
server_socket.bind((DNS_ADDRESS, DNS_PORT))
30+
31+
print("Server Socket initialized.")
32+
server_socket.listen()
33+
34+
while 1:
35+
socket_client, addr_client = server_socket.accept()
36+
print(f"Established connection with {addr_client}")
37+
Thread(target=handle_request, args=(socket_client,)).start()
38+
39+
main()

src/env.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DNS_ADDRESS = "localhost"
2+
DNS_PORT = 8888

src/main_server.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from utils import http_parser_request
2+
3+
from socket import socket, AF_INET, SOCK_STREAM
4+
from threading import Thread
5+
6+
def http_get(socket_client):
7+
msgHeader = 'HTTP/1.1 200 OK \r\n' \
8+
'Date: Tue, 09 Aug 2022 13:23:35 GMT\r\n' \
9+
'Server: MyServer/0.0.1\r\n' \
10+
'Content-Type: text/html\r\n' \
11+
'\r\n'
12+
msgBody = '<html>' \
13+
'<head><title>Hello, World</title></head>' \
14+
'<body><h1> Your first web server!</h1>' \
15+
'<h3>Congratulation!!</h3>' \
16+
'</body>' \
17+
'</html>'
18+
19+
msgHtml = msgHeader + msgBody
20+
21+
socket_client.send(msgHtml.encode())
22+
23+
def http_post():
24+
...
25+
26+
def handle_request(socket_client):
27+
req = socket_client.recv(2048).decode()
28+
if(req == ''):
29+
socket_client.close()
30+
return
31+
32+
request = http_parser_request(req)
33+
34+
if(request['Method'] == 'GET'):
35+
http_get(socket_client)
36+
37+
elif(request['Method'] == 'POST'):
38+
http_post()
39+
40+
return
41+
42+
def main():
43+
server_socket = socket(AF_INET, SOCK_STREAM)
44+
server_socket.bind(("localhost", 1011))
45+
46+
print("Server Socket initialized.")
47+
server_socket.listen()
48+
49+
while 1:
50+
socket_client, addr_client = server_socket.accept()
51+
print(f"Established connection with {addr_client}")
52+
Thread(target=handle_request, args=(socket_client,)).start()
53+
54+
if __name__ == '__main__':
55+
main()

src/utils.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import json
2+
3+
def readjson():
4+
f = open('./data/dns.json', 'r')
5+
data = json.load(f)
6+
f.close()
7+
return data
8+
9+
def http_parser_request(html_string):
10+
11+
map = dict()
12+
html_string = html_string.split('\r\n')
13+
14+
map['body'] = ''
15+
16+
first = True
17+
is_body = False
18+
19+
for i in (html_string):
20+
if (first):
21+
i = i.split(' ')
22+
map['Method'] = i[0]
23+
map['Path'] = i[1]
24+
map['Version'] = i[2]
25+
first = False
26+
27+
elif not is_body:
28+
if (i == ''):
29+
is_body = True
30+
else:
31+
i = i.split(':')
32+
map[i[0]] = i[1]
33+
34+
else:
35+
map['body'] += i +'\n'
36+
37+
return map
38+
39+
def http_parser_reply(html_string):
40+
41+
map = dict()
42+
html_string = html_string.split('\r\n')
43+
44+
map['body'] = ''
45+
46+
first = True
47+
is_body = False
48+
49+
for i in (html_string):
50+
if (first):
51+
i = i.split(' ')
52+
map['Version'] = i[0]
53+
map['Status Code'] = i[1]
54+
map['Status Message'] = i[2]
55+
first = False
56+
57+
elif not is_body:
58+
if (i == ''):
59+
is_body = True
60+
else:
61+
i = i.split(':')
62+
map[i[0]] = i[1]
63+
64+
else:
65+
map['body'] += i +'\n'
66+
67+
return map

0 commit comments

Comments
 (0)