当前位置: 首页>>代码示例>>Python>>正文


Python Action.resource方法代码示例

本文整理汇总了Python中action.Action.resource方法的典型用法代码示例。如果您正苦于以下问题:Python Action.resource方法的具体用法?Python Action.resource怎么用?Python Action.resource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在action.Action的用法示例。


在下文中一共展示了Action.resource方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from action import Action [as 别名]
# 或者: from action.Action import resource [as 别名]
class Server:
    def __init__(self, port):
        #initiate a localhost server
        #TODO refactor for deployment on different IPs
        self.ip_address = '127.0.0.1'
        self.port = port
        self.sock = None
        self.resource = {"available": True, "amount": 100}
        self.myselfAsComputer = Computer(self.ip_address, self.port)
        self.known_computers = {} # {'<ip>_<port>':<Computer>}
        self.crack_tasks = {} # {'crack_task_id':<CrackTask>}

        self.connection = None

        self.resource_responded_task_ids = set()  # {'<task_id>'}
        # Commands: resource[GET], resourcereply[POST], checkmd5[POST], answermd5[POST], crack[GET]
        # Action handlers. Usage: self.handle.<command>(params)
        self.handle = Action()

        self.addComputersFromMachinesTxt()

    def addComputersFromMachinesTxt(self):
        try:
            filename = 'machines/machines%s.txt' % self.port
            input_file = open(filename, 'r')
            # Read string representitive of machine list.
            machines = input_file.read()
            # Parse string to list.
            machines = ast.literal_eval(machines)
            computers = [Computer(machine[0], machine[1]) for machine in machines]
            self.addComputers(computers)
        except:
            raise


    def addComputers(self, computers):
        for computer in computers:
            ip = computer.ip_address
            port = computer.port
            key = "%s_%s" % (ip, port)
            if key not in self.known_computers.keys():
                self.known_computers[key] = computer


    def boot(self):
        self.address = (self.ip_address, self.port)
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.sock.bind(self.address)
        return None

    def listen(self):
        self.sock.listen(10)
        while True:
            print 'waiting for a connection'
            connection, client_address = self.sock.accept()
            try:
                print 'connection from', client_address
                request = connection.recv(32768)

                parse_result = self.parseRequestHeader(request)
                print 'parse_result >>>', parse_result
                method, command, params = parse_result

                if command == "/crack":
                    print "dealing with a browser client"
                    connection.send('HTTP/1.1 200 OK\n')
                    connection.send('Content-Type: text/html; encoding=utf8\n')
                    #TODO maybe add a time-out header
                    connection.send('Connection: keep-alive\n')
                    connection.send('\n') # to separate headers from body
                    connection.send('<html><body>')
                    connection.send('<h1>Hello, %s:%i!</h1>' %(client_address))
                    connection.send('<h2>Requested MD5: %s</h2>' %(params['md5'][0]))
                    connection.send('\n\n')

                    if not self.connection:
                        self.connection = connection
                        print "created self.connection"
                    else:
                        print "Master server is busy atm"
                        connection.send('<p> This server is already busy with another thread. Try again later.\n')
                        connection.close()
                        continue

                else:
                    print "dealing with inside requests"
                    connection.send("OK")
                    connection.close()

                t = threading.Thread(target=self.handleRequest, args=(parse_result,))
                t.start()
            except:
                print >>sys.stderr, "An error has occured while listening." , sys.exc_info()

        return None

    def handleRequest(self, query_data):
        method, command, params = query_data
        print "inside handleRequest() thread version"
#.........这里部分代码省略.........
开发者ID:agraver,项目名称:P2MD5,代码行数:103,代码来源:server.py


注:本文中的action.Action.resource方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。