示例#1
1
    def locate(self, spin, header, fd, resource, version):
        path = join(self.path, basename(resource))

        if not isfile(path):
            return

        # This is used to tell rapidserv reactor that 
        # the connection will keep alive to process
        # sending of data.
        spin.ACTIVE = True

        # Where we are going to serve files.
        # I might spawn an event like FILE_NOT_FOUND.
        # So, users could use it to send appropriate answers.
        type_file, encoding = guess_type(path)
        default_type = 'application/octet-stream'

        header = Header()
        header.set_response('HTTP/1.1 200 OK')

        header.add_header(('Content-Type', type_file if type_file else default_type),
                     ('Content-Length', getsize(path)))

        # Start sending the header.
        spin.dump(str(header))

        # Wait to dump the header.
        xmap(spin, DUMPED, lambda con: drop(con, path))

        # Wait to dump the file.

        xmap(spin, DUMPED_FILE, lose)
        xmap(spin, OPEN_FILE_ERR, lambda con, err: lose(con))
示例#2
0
    def spawn_request(self):
        sched.unmark(self.TIMEOUT, self.spawn_idle_timeout)

        spawn(self.spin, self.request[0], self.header, self.fd,
                                    self.request[1], self.request[2])

        if not self.spin.ACTIVE:
            lose(self.spin)
示例#3
0
文件: utils.py 项目: lowks/untwisted
def set_up_con(con, data):
    Stdin(con)
    Stdout(con)
    
    HttpClient(con)
    xmap(con, CLOSE, lambda con, err: lose(con))
    con.dump(data)
示例#4
0
def set_up_con(con, data):
    Stdin(con)
    Stdout(con)

    HttpClient(con)
    xmap(con, CLOSE, lambda con, err: lose(con))
    con.dump(data)
示例#5
0
文件: ircmode.py 项目: joodicator/vy
    def set_up_con(self, con, area):
        Stdin(con)
        Stdout(con)
        Shrug(con)
        Irc(con)

        xmap(con, CLOSE, lambda con, err: lose(con))
        self.set_common_irc_handles(area, con)
        self.set_common_irc_commands(area, con)
示例#6
0
文件: simple.py 项目: lowks/untwisted
def set_up_con(con, data):
    Stdin(con)
    Stdout(con)
    
    HttpClient(con)
    xmap(con, HTTP_RESPONSE, handle_http_response)
    xmap(con, CLOSE, lambda con, err: lose(con))

    con.dump(data)
示例#7
0
def send_response(spin, response):
    """
    Used to dump a Response/Header object to the client as well
    as all object whose method __str__ is implemented.
    """

    spin.ACTIVE = True
    spin.dump(str(response))
    xmap(spin, DUMPED, lambda con: lose(con))
示例#8
0
文件: simple.py 项目: lowks/untwisted
def set_up_con(con, data):
    Stdin(con)
    Stdout(con)

    HttpClient(con)
    xmap(con, HTTP_RESPONSE, handle_http_response)
    xmap(con, CLOSE, lambda con, err: lose(con))

    con.dump(data)
示例#9
0
文件: basic.py 项目: mfferreira/uxreq
def connect(rsc, username, password):
    ADDR = 'baas.kinvey.com'
    PORT = 80
    HEADER = {'Host': '%s' % ADDR, 'Authorization': auth(username, password)}

    con = Spin()
    data = get(rsc, header=HEADER)

    Client(con)
    con.connect_ex((ADDR, PORT))

    xmap(con, CONNECT, set_up_con, data)
    xmap(con, CONNECT_ERR, lambda con, err: lose(con))

    return con
示例#10
0
文件: simple.py 项目: lowks/untwisted
def connect(addr, port, rsc):
    HEADER = {
        'Host': '%s' % addr,
        'User-Agent': "uxreq/1.0",
        'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
        'Connection': 'close',
    }

    con = Spin()
    data = get(rsc, header=HEADER)

    Client(con)
    con.connect_ex((addr, port))
    xmap(con, CONNECT, set_up_con, data)
    xmap(con, CONNECT_ERR, lambda con, err: lose(con))

    return con
示例#11
0
文件: simple.py 项目: lowks/untwisted
def connect(addr, port, rsc):
    HEADER = {
    'Host':'%s' % addr,
    'User-Agent':"uxreq/1.0", 
    'Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
    'Connection':'close',
    }
        
    
    con  = Spin()
    data = get(rsc, header=HEADER)
    
    Client(con)
    con.connect_ex((addr, port))
    xmap(con, CONNECT, set_up_con, data)
    xmap(con, CONNECT_ERR, lambda con, err: lose(con))

    return con
示例#12
0
文件: post.py 项目: mfferreira/uxreq
def connect(rsc, username, password, payload):
    ADDR   = 'baas.kinvey.com'
    PORT   = 80
    HEADER = {
    'Host':'%s' % ADDR,
    'Authorization': auth(username, password)
    }
        
    con  = Spin()
    data = post_data(rsc, data=json.loads(payload), header=HEADER)
    
    Client(con)
    con.connect_ex((ADDR, PORT))

    xmap(con, CONNECT, set_up_con, data)
    xmap(con, CONNECT_ERR, lambda con, err: lose(con))

    return con
示例#13
0
文件: ircbot.py 项目: lowks/untwisted
def on_connect(con):
    # This protocol is responsible by spawning 
    # the event LOAD. It takes care of spawning a CLOSE
    # event when the connection is over.
    Stdout(con)
    
    # This protocol is responsible by installing a dump method
    # into the con instance. It takes care of sending everything
    # that goes through the dump method.
    Stdin(con)


    # This protocol is used to break the stream of data into chunks delimited
    # by '\r\n'. So, if the network sends 'data1\r\ndata2\r\ndata3\r\n' it will
    # spawns three times the event FOUND. It will carry 'data1', 'data2', 'data3'.
    Shrug(con)

    # This untwisted protocol is a tiny implementation of irc protocol.
    # It handles about 80% of the irc events. It is possible to be improved
    # and handle 100% of all irc events.
    Irc(con)

    # We want to print out on the screen all data that comes from the irc server.
    xmap(con, LOAD, lambda con, data: sys.stdout.write('%s\n' % data))

    # When the connection is over we need to destroy the Spin instance. The
    # lose function takes care of doing that for us.
    xmap(con, CLOSE, lambda con, err: lose(con))

    # When the event 'PRIVMSG' happens then we will have our handle
    # on_privmsg called. You could experiment other irc commands.
    xmap(con, 'PRIVMSG', on_privmsg)

    # When the irc server requires us to send back a PONG :server.
    xmap(con, 'PING', on_ping)

    # Our nick.
    con.dump('NICK %s\r\n' % NICK)
    
    con.dump('USER %s %s %s :%s\r\n' % (IRC_X, IRC_Y, IRC_Z, IRC_W))

    # Finally, it joins the channel.
    con.dump('JOIN %s\r\n' % CHANNEL)
示例#14
0
文件: ircbot.py 项目: lowks/untwisted
def on_connect(con):
    # This protocol is responsible by spawning
    # the event LOAD. It takes care of spawning a CLOSE
    # event when the connection is over.
    Stdout(con)

    # This protocol is responsible by installing a dump method
    # into the con instance. It takes care of sending everything
    # that goes through the dump method.
    Stdin(con)

    # This protocol is used to break the stream of data into chunks delimited
    # by '\r\n'. So, if the network sends 'data1\r\ndata2\r\ndata3\r\n' it will
    # spawns three times the event FOUND. It will carry 'data1', 'data2', 'data3'.
    Shrug(con)

    # This untwisted protocol is a tiny implementation of irc protocol.
    # It handles about 80% of the irc events. It is possible to be improved
    # and handle 100% of all irc events.
    Irc(con)

    # We want to print out on the screen all data that comes from the irc server.
    xmap(con, LOAD, lambda con, data: sys.stdout.write('%s\n' % data))

    # When the connection is over we need to destroy the Spin instance. The
    # lose function takes care of doing that for us.
    xmap(con, CLOSE, lambda con, err: lose(con))

    # When the event 'PRIVMSG' happens then we will have our handle
    # on_privmsg called. You could experiment other irc commands.
    xmap(con, 'PRIVMSG', on_privmsg)

    # When the irc server requires us to send back a PONG :server.
    xmap(con, 'PING', on_ping)

    # Our nick.
    con.dump('NICK %s\r\n' % NICK)

    con.dump('USER %s %s %s :%s\r\n' % (IRC_X, IRC_Y, IRC_Z, IRC_W))

    # Finally, it joins the channel.
    con.dump('JOIN %s\r\n' % CHANNEL)
示例#15
0
    def handle_accept(self, local, client):
        """
        This method is not supposed to be called by functions outside
        this class.
        """

        Stdin(client)
        Stdout(client)

        HttpServer(client)
        Get(client)
        Post(client)

        # It serves to determine whether the client made
        # a request whose resource exists.
        # In case it didnt the connection is dropped.
        client.ACTIVE = False

        for ind in self.setup:
            ind(client)

        xmap(client, CLOSE, lambda con, err: lose(con))
示例#16
0
文件: is_up.py 项目: lowks/untwisted
def on_connect_err(con, err):
    print "It couldn't connect, errcode ", errno.errorcode[err]
    lose(con)
示例#17
0
""" Author: Jayrays
    Description: A simple port scan that verifies for listening sockets.
"""

from untwisted.network import *
from untwisted.utils.stdio import Client, lose, CONNECT, CONNECT_ERR
from socket import *
from sys import argv

script, ip, low, high = argv


def is_open(spin, port):
    print 'Port %s is open.' % port


for ind in range(int(low), int(high)):
    sock = socket(AF_INET, SOCK_STREAM)
    spin = Spin(sock)
    Client(spin)
    xmap(spin, CONNECT, is_open, ind)
    xmap(spin, CONNECT_ERR, lambda con, err: lose(con))
    xmap(spin, CONNECT, lose)
    spin.connect_ex((ip, ind))

core.gear.mainloop()
示例#18
0
def on_connect_err(con, err):
    print "It couldn't connect, errcode ", errno.errorcode[err]
    lose(con)
示例#19
0
文件: bar.py 项目: lowks/untwisted
def set_up_con(server, con):
    Stdout(con)
    xmap(con, CLOSE, lambda con, err: lose(con))
    xmap(con, LOAD, lambda con, data: sys.stdout.write('%s\n' % data))
示例#20
0
文件: scan.py 项目: lowks/untwisted
""" Author: Jayrays
    Description: A simple port scan that verifies for listening sockets.
"""

from untwisted.network import *
from untwisted.utils.stdio import Client, lose, CONNECT, CONNECT_ERR
from socket import *
from sys import argv

script, ip, low, high = argv

def is_open(spin, port):
    print 'Port %s is open.' % port

for ind in range(int(low), int(high)):
    sock = socket(AF_INET, SOCK_STREAM)
    spin = Spin(sock)
    Client(spin)
    xmap(spin, CONNECT, is_open, ind)
    xmap(spin, CONNECT_ERR, lambda con, err: lose(con))
    xmap(spin, CONNECT, lose)
    spin.connect_ex((ip, ind))


core.gear.mainloop()
示例#21
0
文件: bar.py 项目: lowks/untwisted
def set_up_con(server, con):
    Stdout(con)
    xmap(con, CLOSE, lambda con, err: lose(con))
    xmap(con, LOAD, lambda con, data: sys.stdout.write('%s\n' % data))