Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env pytho
- from http.server import BaseHTTPRequestHandler
- from urllib import parse
- class Parser():
- def __init__(self,texto):
- self.texto=texto
- def analisis(self):
- #Crea una lista [] entre basada en '&'
- lista=(self.texto).split("&")
- #Crear un diccionario desde la lista mira el simbolo '='
- diccionario = {}
- for item in lista:
- diccionario[item.split('=')[0]] = item.split('=')[1]
- #ver diccionario
- print ("--------------------------------------")
- for clave, item in diccionario.items():
- print (clave, ":", item)
- print ("--------------------------------------")
- class GetHandler(BaseHTTPRequestHandler):
- def do_GET(self):
- parsed_path = parse.urlparse(self.path)
- message_parts = [
- 'CLIENT VALUES:',
- 'client_address={} ({})'.format(
- self.client_address,
- self.address_string()),
- 'command={}'.format(self.command), #Contiene el comando por ejemplo GET
- 'requestline={}'.format(self.requestline),
- 'path={}'.format(self.path), #Contiene la ruta de solicitud
- 'real path={}'.format(parsed_path.path),
- 'query={}'.format(parsed_path.query),
- 'request_version={}'.format(self.request_version),# Contiene version de respuesta p.e HTTP/1.0
- '',
- 'SERVER VALUES:',
- 'server_version={}'.format(self.server_version),#Especifica la version del servidor p.e Python/1.4
- 'sys_version={}'.format(self.sys_version),
- 'protocol_version={}'.format(self.protocol_version),
- '',
- 'HEADERS RECEIVED:',
- ]
- #Aqui obtengo la peticion GET de Vauban
- #print ("\nRequest line = \n"+str ((self.requestline).split())+"\n \n")
- """
- Request line =
- GET /?EventDate=2019-01-24T14:25:27&UserName=SCOTH&UserFirstName=BON&Identifier=49493&ReaderName=Porte%20badge&EventType=Utilisateur%20interdit&EventColor=0&EventTypeNumber=12&PlaqueLPR=&EventId=371 HTTP/1.1
- """
- #Analisis de la request utiliza la clase Parser crea un diccionario de claves y valores
- p=Parser(str(self.requestline))
- p.analisis()
- for name, value in sorted(self.headers.items()):
- message_parts.append(
- '{}={}'.format(name, value.rstrip())
- )
- message_parts.append('')
- message = '\r\n'.join(message_parts)
- #self.send_response(200)
- self.send_header('Content-Type',
- 'text/plain; charset=utf-8')
- self.end_headers()
- #The message text is assembled and then written to wfile
- self.wfile.write(message.encode('utf-8'))
- if __name__ == '__main__':
- from http.server import HTTPServer
- server = HTTPServer(('192.168.6.150', 8888), GetHandler)
- print('Starting server, use <Ctrl-C> to stop')
- server.serve_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement