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))
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)
def set_up_con(con, data): Stdin(con) Stdout(con) HttpClient(con) xmap(con, CLOSE, lambda con, err: lose(con)) con.dump(data)
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)
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)
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))
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
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
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
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
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)
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))
def on_connect_err(con, err): print "It couldn't connect, errcode ", errno.errorcode[err] lose(con)
""" 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()
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))