Exemplo n.º 1
0
Arquivo: Topology.py Projeto: smbz/vns
class WebServer(BasicNode):
    """A host in the network which is serving a website (specified by the
    web_server_to_proxy_hostname parameter) on TCP port 80.  Like
    Host, it also replies to echo and ARP requests.  It serves the specified
    website by acting as a proxy for that website."""
    def __init__(self, topo, name, path_to_serve):
        BasicNode.__init__(self, topo, name)
        self.http_server = HTTPServer(TCPServer.ANY_PORT, path_to_serve)
        self.intf = None
        self.pkt = None

    @staticmethod
    def get_type_str():
        return 'Web Server'

    def handle_non_icmp_ip_packet_to_self(self, intf, pkt):
        """If pkt is to an HTTP_PORT, then the packet is handed off to the HTTP
        server.  Otherwise, the default superclass implementation is called."""
        reactor.callLater(2, self.send_packets)
        if pkt.is_valid_tcp():
            if is_http_port(pkt.tcp_dst_port):
                self.handle_http_request(intf, pkt)
                return
        BasicNode.handle_non_icmp_ip_packet_to_self(self, intf, pkt)

    def handle_http_request(self, intf, pkt):
        """Forward the rceived packet from an HTTP client to the web server."""
        self.intf = intf
        self.pkt = pkt
        tcp_conn = self.http_server.handle_tcp(pkt)
        self.send_packets()

    def send_packets(self):
        """Send any waiting packets"""
        if self.intf and self.pkt:
            for (_, tcp_conn) in self.http_server.connections.iteritems():
                if tcp_conn and not tcp_conn.dead:
                    tcp_pts = tcp_conn.get_packets_to_send()
                    for tcp, data in tcp_pts:
                        eth = self.pkt.get_reversed_eth()
                        ip = self.pkt.get_reversed_ip(new_ttl=64, new_tlen=self.pkt.ip_hlen+len(tcp)+len(data))
                        pkt_out = eth + ip + Packet.cksum_tcp_hdr(ip, tcp, data) + data
                        logging.debug('%s sending packet from HTTP server: %s' % (self, pktstr(pkt_out)))
                        self.intf.link.send_to_other(self.intf, pkt_out)

    def __str__(self):
        ps = ' serving:%s' % self.http_server.get_path_being_served()
        return BasicNode.__str__(self) + ps
Exemplo n.º 2
0
def start():
    global http_thread
    try:
        addr = ("", int(mt.config["http/port"]))
        server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        server_socket.bind(addr)
        server_socket.listen(10)
        server_socket.settimeout(0.1)

        http_thread = HTTPServer(server_socket)
        http_thread.start()
        return True

    except Exception as inst:
        pass

    return False
Exemplo n.º 3
0
class WebServer(BasicNode):
    """A host in the network which is serving a website (specified by the
    web_server_to_proxy_hostname parameter) on TCP port 80.  Like
    Host, it also replies to echo and ARP requests.  It serves the specified
    website by acting as a proxy for that website."""
    def __init__(self, topo, name, path_to_serve):
        BasicNode.__init__(self, topo, name)
        self.http_server = HTTPServer(TCPServer.ANY_PORT, path_to_serve)

    @staticmethod
    def get_type_str():
        return 'Web Server'

    def handle_non_icmp_ip_packet_to_self(self, intf, pkt):
        """If pkt is to an HTTP_PORT, then the packet is handed off to the HTTP
        server.  Otherwise, the default superclass implementation is called."""
        if pkt.is_valid_tcp():
            if is_http_port(pkt.tcp_dst_port):
                self.handle_http_request(intf, pkt)
                return
        BasicNode.handle_non_icmp_ip_packet_to_self(self, intf, pkt)

    def handle_http_request(self, intf, pkt):
        """Forward the received packet from an HTTP client to the web server."""
        tcp_conn = self.http_server.handle_tcp(pkt)
        if tcp_conn:
            tcp_pts = tcp_conn.get_packets_to_send()
            if tcp_pts:
                for tcp, data in tcp_pts:
                    eth = pkt.get_reversed_eth()
                    ip = pkt.get_reversed_ip(new_ttl=64, new_tlen=pkt.ip_hlen+len(tcp)+len(data))
                    pkt_out = eth + ip + Packet.cksum_tcp_hdr(ip, tcp, data) + data
                    logging.debug('%s sending packet from HTTP server: %s' % (self, pktstr(pkt_out)))
                    intf.link.send_to_other(intf, pkt_out)

    def __str__(self):
        ps = ' serving:%s' % self.http_server.get_path_being_served()
        return BasicNode.__str__(self) + ps
Exemplo n.º 4
0
def main():

    parser = argparse.ArgumentParser(description="HTTP Server")

    parser.add_argument(
        "port_number",
        type=int,
        help="Port Number for Server to listen on.\nDefault Port is 80",
        default=False)

    args = parser.parse_args()

    if args.port_number:
        s = HTTPServer(args.port_number)
    else:
        s = HTTPServer()
    s.start()
Exemplo n.º 5
0
    global msgs
    page = get_page()
    page = page.replace("{nerodia}", str(Nerodia))
    page = page.replace("{message}", "<br>".join(msgs))
    msgs = []
    return build_response(body=page)


def main_page(request):
    return build_console()


def motor_endpoint(request):
    global msgs

    endpoint = request['path'].split("/")[-1]
    if endpoint == 'open':
        Nerodia.open()
    elif endpoint == 'close':
        Nerodia.close()
    else:
        msgs.append("{} not found".format(endpoint))
    return build_console()


nerodia_server = HTTPServer(handlers={
    "/": main_page,
    "/open": motor_endpoint,
    "/close": motor_endpoint,
})
Exemplo n.º 6
0
 def run_server(self, port, **args):
     from .HTTPServer import HTTPServer
     srv = HTTPServer(port, self, **args)
     srv.start()
     srv.join()
Exemplo n.º 7
0
Arquivo: Topology.py Projeto: smbz/vns
 def __init__(self, topo, name, path_to_serve):
     BasicNode.__init__(self, topo, name)
     self.http_server = HTTPServer(TCPServer.ANY_PORT, path_to_serve)
     self.intf = None
     self.pkt = None
Exemplo n.º 8
0
from HTTPServer import HTTPServer

if __name__ == '__main__':
    server = HTTPServer()
    server.start()
Exemplo n.º 9
0
import socket

from WebSocketServer import WebSocketInServer
from WebSocketServer import WebSocketFaceServer, WebSocketControllerServer
from WebSocketServer import wsEvents, wsFaceCommands, wsControllerCommands
from HTTPServer import HTTPServer
from ConsoleLog import normal, error, notice
from InteractionController import InteractionController
from MusicAnalyzer import NoteGrouper, PhraseCutter
from UDPServer import UDPServer
from SerialServer import SerialServer

wsInServer = WebSocketInServer(8000)
wsFaceServer = WebSocketFaceServer(8001)
wsControllerServer = WebSocketControllerServer(8002)
httpServer = HTTPServer(8006)
udpServer = UDPServer()
serialServer = SerialServer()

wsInServer.start()
wsFaceServer.start()
wsControllerServer.start()
httpServer.start()
udpServer.start()
serialServer.start()

interactionController = InteractionController()

midiIn = rtmidi.RtMidiIn()
midiOut = rtmidi.RtMidiOut()
noteGrouper = NoteGrouper()
Exemplo n.º 10
0
 def __init__(self, topo, name, path_to_serve):
     BasicNode.__init__(self, topo, name)
     self.http_server = HTTPServer(TCPServer.ANY_PORT, path_to_serve)