Last active
September 16, 2018 16:17
-
-
Save thotypous/10a315490c9c16f0d648f8357e90a349 to your computer and use it in GitHub Desktop.
Exemplos de implementação de camada de aplicação sobre TCP
This file contains hidden or 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
#!/usr/bin/python3 | |
# -*- encoding: utf-8 -*- | |
import socket | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect(('endereco', 8000)) | |
s.send(b'A'*50000 + b'F') | |
s.close() |
This file contains hidden or 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
#!/usr/bin/python3 | |
# -*- encoding: utf-8 -*- | |
import socket | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
s.bind(('', 8000)) | |
s.listen(1) | |
while True: | |
cli, addr = s.accept() | |
dados = b'' | |
contador = 0 | |
while not b'F' in dados: # experimente não colocar este while | |
dados += cli.recv(1000000) | |
contador += 1 | |
print('Recebi %d bytes' % len(dados)) | |
print('Executei recv() %d vezes' % contador) |
This file contains hidden or 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
#!/usr/bin/python3 | |
# -*- encoding: utf-8 -*- | |
import socket, select | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
s.bind(('', 8000)) | |
s.listen(1) | |
s.setblocking(0) | |
clientes = [] | |
reqs = {} | |
while True: | |
rlist, wlist, xlist = select.select(clientes + [s], [], []) | |
print(rlist) | |
for cli in rlist: | |
if cli == s: | |
cli, addr = s.accept() | |
cli.setblocking(0) | |
clientes.append(cli) | |
reqs[cli] = b'' | |
else: | |
reqs[cli] += cli.recv(1500) | |
req = reqs[cli] | |
if b'\r\n\r\n' in req or b'\n\n' in req: | |
method, path, lixo = req.split(b' ', 2) | |
if method == b'GET': | |
texto = b"Hello " + path | |
else: | |
texto = b"Num entendi" | |
resp = b"HTTP/1.0 200 OK\r\nContent-Length: %d\r\n\r\n" % len(texto) | |
resp += texto | |
# note que um bom servidor usaria também a wlist e enviaria a resposta por pedaços | |
cli.send(resp) | |
cli.close() | |
del reqs[cli] | |
clientes.remove(cli) |
This file contains hidden or 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
#!/usr/bin/python3 | |
# -*- encoding: utf-8 -*- | |
import socket | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
s.bind(('', 8000)) | |
s.listen(1) | |
while True: | |
cli, addr = s.accept() | |
while True: | |
req = b'' | |
while not (b'\r\n\r\n' in req or b'\n\n' in req): | |
pedaco = cli.recv(1500) | |
if pedaco == b'': | |
break | |
req += pedaco | |
if req == b'': | |
break | |
print(req) | |
print('requisição tem %d bytes' % len(req)) | |
metodo, caminho, lixo = req.split(b' ', 2) | |
if caminho.endswith(b'.js'): | |
nome = caminho.replace(b'/', b'').replace(b'.js', b'') | |
corpo = b'alert("oi, %s");' % nome | |
cli.send(b'HTTP/1.1 200 OK\r\nContent-Type: text/javascript\r\nContent-Length: %d\r\n\r\n' % len(corpo)) | |
cli.send(corpo) | |
else: | |
corpo = b'<script src="%s.js"></script>' % caminho | |
cli.send(b'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n' % len(corpo)) | |
cli.send(corpo) | |
cli.close() | |
print('<conexao fechada>') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment