예제 #1
0
    def request(self, method, host, schema="https", path="/", headers={}, data="", timeout=10):
        url = schema + "://" + host + path
        payloads = ['%s %s HTTP/1.1\r\n' % (method, url)]
        for k in headers:
            v = headers[k]
            payloads.append('%s: %s\r\n' % (k, v))
        head_payload = "".join(payloads)

        request_body = '%s%s%s%s' % \
                       ((struct.pack('!H', len(head_payload)),  head_payload,
                         struct.pack('!I', len(data)), data))
        request_headers = {'Content-Length': len(data), 'Content-Type': 'application/octet-stream'}

        content, status, response = self._request(
            "POST", "xxnet10.herokuapp.com", "/2/index.php",
            request_headers, request_body, timeout)

        #xlog.info('%s "PHP %s %s %s" %s %s', handler.address_string(), handler.command, url, handler.protocol_version, response.status, response.getheader('Content-Length', '-'))
        # xlog.debug("status:%d", status)
        if status == 200:
            self.last_success_time = time.time()
            self.continue_fail_num = 0
        else:
            self.last_fail_time = time.time()
            self.continue_fail_num += 1

        try:
            res = simple_http_client.TxtResponse(content)
        except:
            return "", 501, {}

        res.worker = response.worker
        res.task = response.task
        return res.body, res.status, res
예제 #2
0
파일: front.py 프로젝트: liuheping/XX-Net
    def request(self,
                method,
                host,
                schema="http",
                path="/",
                headers={},
                data="",
                timeout=40):
        # change top domain to xx-net.net
        # this domain bypass the cloudflare front for ipv4
        p = host.find(".")
        host_sub = host[:p]
        host = host_sub + ".xx-net.net"

        schema = "http"
        # force schema to http, avoid cert fail on heroku curl.
        # and all x-server provide ipv4 access

        url = schema + "://" + host + path
        payloads = ['%s %s HTTP/1.1\r\n' % (method, url)]
        for k in headers:
            v = headers[k]
            payloads.append('%s: %s\r\n' % (k, v))
        head_payload = "".join(payloads)

        request_body = '%s%s%s%s' % \
                       ((struct.pack('!H', len(head_payload)),  head_payload,
                         struct.pack('!I', len(data)), data))
        request_headers = {
            'Content-Length': len(data),
            'Content-Type': 'application/octet-stream'
        }

        heroku_host = str(random.choice(self.hosts))
        content, status, response = self._request("POST", heroku_host, "/2/",
                                                  request_headers,
                                                  request_body, timeout)

        #xlog.info('%s "PHP %s %s %s" %s %s', handler.address_string(), handler.command, url, handler.protocol_version, response.status, response.getheader('Content-Length', '-'))
        # xlog.debug("status:%d", status)
        if status == 200:
            xlog.debug("%s %s%s trace:%s", method, host, path,
                       response.task.get_trace())
            self.last_success_time = time.time()
            self.continue_fail_num = 0
            self.success_num += 1
        else:
            self.last_fail_time = time.time()
            self.continue_fail_num += 1
            self.fail_num += 1

        try:
            res = simple_http_client.TxtResponse(content)
        except:
            return "", 501, {}

        res.worker = response.worker
        res.task = response.task
        return res.body, res.status, res
예제 #3
0
    def request(self,
                method,
                host,
                schema="http",
                path="/",
                headers={},
                data="",
                timeout=40):
        schema = "http"
        # force schema to http, avoid cert fail on heroku curl.
        # and all x-server provide ipv4 access

        url = schema + "://" + host + path
        payloads = ['%s %s HTTP/1.1\r\n' % (method, url)]
        for k in headers:
            v = headers[k]
            payloads.append('%s: %s\r\n' % (k, v))
        head_payload = "".join(payloads)

        request_body = '%s%s%s%s' % \
                       ((struct.pack('!H', len(head_payload)),  head_payload,
                         struct.pack('!I', len(data)), data))
        request_headers = {
            'Content-Length': len(data),
            'Content-Type': 'application/octet-stream'
        }

        heroku_host = ""
        content, status, response = self._request("POST", heroku_host, "/2/",
                                                  request_headers,
                                                  request_body, timeout)

        # self.logger.info('%s "PHP %s %s %s" %s %s', handler.address_string(), handler.command, url, handler.protocol_version, response.status, response.getheader('Content-Length', '-'))
        # self.logger.debug("status:%d", status)
        if status == 200:
            self.logger.debug("%s %s%s trace:%s", method, host, path,
                              response.task.get_trace())
        else:
            if status == 404:
                heroku_host = response.ssl_sock.host
                self.logger.warn("heroku:%s fail", heroku_host)
                try:
                    self.host_manager.remove(heroku_host)
                except:
                    pass

        try:
            res = simple_http_client.TxtResponse(content)
        except Exception as e:
            self.logger.exception("decode %s response except:%r", content, e)
            return "", 501, {}

        res.worker = response.worker
        res.task = response.task
        return res.body, res.status, res