KunterBuntesSeminar-SS10/Breaking the DNS for fun and profit/Echo-Server-DNS-Authenticated.py

Aus Fachschaft_Informatik
Zur Navigation springen Zur Suche springen
Die druckbare Version wird nicht mehr unterstützt und kann Darstellungsfehler aufweisen. Bitte aktualisiere deine Browser-Lesezeichen und verwende stattdessen die Standard-Druckfunktion des Browsers.
#!/usr/bin/env python

"""
An echo server that uses select to handle multiple clients at a time.
Entering any line of input at the terminal will exit the server.
Access control! Only bob.anti-positiv.de can access.
"""

import select
import socket
import sys

host = 'alice.anti-positiv.de'
port = 23421
backlog = 5
size = 1024
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host,port))
server.listen(backlog)
input = [server,sys.stdin]
running = 1
if True:
    while running:
        inputready,outputready,exceptready = select.select(input,[],[])


        for s in inputready:

            if s == server:
                # handle the server socket
                client, address = server.accept()
                input.append(client)
                ip_address = address[0]
                hostname = socket.gethostbyaddr(ip_address)
                if hostname[0] == 'bob.anti-positiv.de':
                   client.send('Welcome bob.anti-positiv.de!\n')
                else:
                   client.send('Access denied to %s (via %s)!\n' % (hostname[0], hostname[2]))
                   client.close()
                   input.remove(client)


            elif s == sys.stdin:
                # handle standard input
                junk = sys.stdin.readline()
                running = 0

            else:
                # handle all other sockets
                data = s.recv(size)
                if data:
                    s.send(data)
                else:
                    s.close()
                    input.remove(s)
server.close()