示例#1
0
文件: client.py 项目: llazzaro/v12
class v12HTTPPageGetter(HTTPClient):

    delimiter = '\n'
    
    def connectionMade(self):
        self.headers = Headers()
        query = ''
        if len(self.factory.query) > 0:
            query = '?' + self.factory.query
        self.sendCommand(self.factory.method, self.factory.path + query)

        # Headers
        for key, values in self.factory.headers.iteritems():
            logging.debug('Header incoming key %s value %s' % (key, values))
            if type(values) == str:
                values = [values]
            for value in values:
                self.sendHeader(key, value)     
        self.endHeaders()      
        # Body
        if self.factory.body is not None:
            self.transport.write(str(self.factory.body))
    
    def lineReceived(self, line):
        return HTTPClient.lineReceived(self, line.rstrip())

    def handleHeader(self, key, value):
        self.headers.add(key, value)

    def handleEndHeaders(self):
        self.factory.gotHeaders(self.headers)
 
    def connectionLost(self, reason):
        HTTPClient.connectionLost(self, reason)
        self.factory.noPage(reason)

    def handleStatus(self, version, status, message):
        logging.debug('Response status %s' % status)
        self.factory.gotStatus(version, status, message)

    def handleResponse(self, response):
        if self.factory.method.upper() == 'HEAD':
            self.factory.page('')
        elif self.length != None and self.length != 0:
            self.factory.noPage(failure.Failure(PartialDownloadError(self.factory.status, None, response)))
        else:
            self.factory.page(response)
        self.transport.loseConnection()

    def timeout(self):
        self.transport.loseConnection()
        self.factory.noPage(\
                defer.TimeoutError("Getting %s took longer than %s seconds." % \
                                            (self.factory.url, self.factory.timeout)))
示例#2
0
 def parse(cls, s):
     if '\r\n\r\n' not in s:
         raise MessageParsingError("Bad message (no CRLFCRLF found)")
     head, content = s.split('\r\n\r\n', 1)
     if not content:
         content = None
     if '\r\n' not in head:
         first_line = head
         hdrs = ''
     else:
         first_line, hdrs = head.split('\r\n', 1)
     headers = Headers.parse(hdrs)
     r = first_line.split(None, 2)
     if len(r) != 3:
         raise MessageParsingError("Bad first line: " + first_line)
     if r[0] == cls.version:
         code = int(r[1])
         reason = r[2]
         return Response(code, reason, headers, content)
     elif r[2] == cls.version:
         method = r[0]
         uri = URI.parse(r[1])
         return Request(method, uri, headers, content)
     else:
         raise MessageParsingError("Bad first line: " + first_line)
示例#3
0
文件: client.py 项目: llazzaro/v12
    def connectionMade(self):
        self.headers = Headers()
        query = ''
        if len(self.factory.query) > 0:
            query = '?' + self.factory.query
        self.sendCommand(self.factory.method, self.factory.path + query)

        # Headers
        for key, values in self.factory.headers.iteritems():
            logging.debug('Header incoming key %s value %s' % (key, values))
            if type(values) == str:
                values = [values]
            for value in values:
                self.sendHeader(key, value)     
        self.endHeaders()      
        # Body
        if self.factory.body is not None:
            self.transport.write(str(self.factory.body))