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


Python HTTPConnection.close方法代码示例

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


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

示例1: compute_engine_id

# 需要导入模块: from six.moves.http_client import HTTPConnection [as 别名]
# 或者: from six.moves.http_client.HTTPConnection import close [as 别名]
def compute_engine_id():
    """Gets the Compute Engine project ID if it can be inferred.

    Uses 169.254.169.254 for the metadata server to avoid request
    latency from DNS lookup.

    See https://cloud.google.com/compute/docs/metadata#metadataserver
    for information about this IP address. (This IP is also used for
    Amazon EC2 instances, so the metadata flavor is crucial.)

    See https://github.com/google/oauth2client/issues/93 for context about
    DNS latency.

    :rtype: string or ``NoneType``
    :returns: Compute Engine project ID if the metadata service is available,
              else ``None``.
    """
    host = '169.254.169.254'
    uri_path = '/computeMetadata/v1/project/project-id'
    headers = {'Metadata-Flavor': 'Google'}
    connection = HTTPConnection(host, timeout=0.1)

    try:
        connection.request('GET', uri_path, headers=headers)
        response = connection.getresponse()
        if response.status == 200:
            return response.read()
    except socket.error:  # socket.timeout or socket.error(64, 'Host is down')
        pass
    finally:
        connection.close()
开发者ID:Loooffy,项目名称:gcloud-python,代码行数:33,代码来源:_implicit_environ.py

示例2: send_email

# 需要导入模块: from six.moves.http_client import HTTPConnection [as 别名]
# 或者: from six.moves.http_client.HTTPConnection import close [as 别名]
def send_email(request):
  try:
    recipients = request.GET['to'].split(',')
    url = request.GET['url']
    proto, server, path, query, frag = urlsplit(url)
    if query: path += '?' + query
    conn = HTTPConnection(server)
    conn.request('GET',path)
    try: # Python 2.7+, use buffering of HTTP responses
      resp = conn.getresponse(buffering=True)
    except TypeError:  # Python 2.6 and older
      resp = conn.getresponse()
    assert resp.status == 200, "Failed HTTP response %s %s" % (resp.status, resp.reason)
    rawData = resp.read()
    conn.close()
    message = MIMEMultipart()
    message['Subject'] = "Graphite Image"
    message['To'] = ', '.join(recipients)
    message['From'] = '[email protected]%s' % gethostname()
    text = MIMEText( "Image generated by the following graphite URL at %s\r\n\r\n%s" % (ctime(),url) )
    image = MIMEImage( rawData )
    image.add_header('Content-Disposition', 'attachment', filename="composer_" + strftime("%b%d_%I%M%p.png"))
    message.attach(text)
    message.attach(image)
    s = SMTP(settings.SMTP_SERVER)
    s.sendmail('[email protected]%s' % gethostname(),recipients,message.as_string())
    s.quit()
    return HttpResponse( "OK" )
  except:
    return HttpResponse( format_exc() )
开发者ID:aihua,项目名称:graphite-web,代码行数:32,代码来源:views.py

示例3: WebcamStreamBase

# 需要导入模块: from six.moves.http_client import HTTPConnection [as 别名]
# 或者: from six.moves.http_client.HTTPConnection import close [as 别名]
class WebcamStreamBase(VideoStream):
    request_headers = {
        'Accept': '*/*',
        }

    def __init__(self, url,
                 max_rate=3.0,
                 rate_bucket_size=None,
                 socket_timeout=10,
                 user_agent=DEFAULT_USER_AGENT):
        self.url = url
        netloc, self.path = _parse_url(url)
        self.conn = HTTPConnection(netloc, timeout=socket_timeout)
        self.request_headers = self.request_headers.copy()
        self.request_headers['User-Agent'] = user_agent

        self.stream = None
        self.rate_limiter = BucketRateLimiter(max_rate, rate_bucket_size)
        self.open_rate_limiter = BackoffRateLimiter(socket_timeout)

    def __repr__(self):
        return u"<%s at 0x%x: %s>" % (
            self.__class__.__name__, id(self), self.url)

    @classmethod
    def from_settings(cls, settings, prefix='webcam.', **defaults):
        config = config_from_settings(settings, prefix=prefix,
                                      subprefix=cls.settings_subprefix,
                                      **defaults)
        return cls(**config)


    def close(self):
        if self.stream:
            self.stream = None
        if self.conn:
            self.conn.close()
            self.conn = None

    @property
    def closed(self):
        return not self.conn

    def next(self):
        # FIXME: check closed more often?
        if self.closed:
            raise StopIteration()
        next(self.rate_limiter)
        try:
            if self.stream is None:
                next(self.open_rate_limiter)
                self.stream = self._open_stream()
            frame = next(self.stream)
            self.open_rate_limiter.reset()
            return frame
        except Exception as ex:
            self.stream = None
            log.warn("Streaming failed: %s", text_type(ex) or repr(ex))
            self.conn.close()
            return None
开发者ID:dairiki,项目名称:puppyserv,代码行数:62,代码来源:webcam.py

示例4: export_to_myexp

# 需要导入模块: from six.moves.http_client import HTTPConnection [as 别名]
# 或者: from six.moves.http_client.HTTPConnection import close [as 别名]
    def export_to_myexp(self, trans, id, myexp_username, myexp_password):
        """
        Exports a workflow to myExperiment website.
        """
        trans.workflow_building_mode = workflow_building_modes.ENABLED
        stored = self.get_stored_workflow(trans, id, check_ownership=False, check_accessible=True)

        # Convert workflow to dict.
        workflow_dict = self._workflow_to_dict(trans, stored)

        #
        # Create and submit workflow myExperiment request.
        #

        # Create workflow content JSON.
        workflow_content = json.dumps(workflow_dict, indent=4, sort_keys=True)

        # Create myExperiment request.
        request_raw = trans.fill_template(
            "workflow/myexp_export.mako",
            workflow_name=workflow_dict['name'],
            workflow_description=workflow_dict['annotation'],
            workflow_content=workflow_content,
            workflow_svg=self._workflow_to_svg_canvas(trans, stored).tostring()
        )
        # strip() b/c myExperiment XML parser doesn't allow white space before XML; utf-8 handles unicode characters.
        request = unicodify(request_raw.strip(), 'utf-8')

        # Do request and get result.
        auth_header = base64.b64encode('%s:%s' % (myexp_username, myexp_password))
        headers = {"Content-type": "text/xml", "Accept": "text/xml", "Authorization": "Basic %s" % auth_header}
        myexp_url = trans.app.config.get("myexperiment_url", self.__myexp_url)
        conn = HTTPConnection(myexp_url)
        # NOTE: blocks web thread.
        conn.request("POST", "/workflow.xml", request, headers)
        response = conn.getresponse()
        response_data = response.read()
        conn.close()

        # Do simple parse of response to see if export successful and provide user feedback.
        parser = SingleTagContentsParser('id')
        parser.feed(response_data)
        myexp_workflow_id = parser.tag_content
        workflow_list_str = " <br>Return to <a href='%s'>workflow list." % url_for(controller='workflows', action='list')
        if myexp_workflow_id:
            return trans.show_message(
                """Workflow '%s' successfully exported to myExperiment. <br/>
                <a href="http://%s/workflows/%s">Click here to view the workflow on myExperiment</a> %s
                """ % (stored.name, myexp_url, myexp_workflow_id, workflow_list_str),
                use_panels=True)
        else:
            return trans.show_error_message(
                "Workflow '%s' could not be exported to myExperiment. Error: %s %s" %
                (stored.name, response_data, workflow_list_str), use_panels=True)
开发者ID:lappsgrid-incubator,项目名称:Galaxy,代码行数:56,代码来源:workflow.py

示例5: _findMatchUrl

# 需要导入模块: from six.moves.http_client import HTTPConnection [as 别名]
# 或者: from six.moves.http_client.HTTPConnection import close [as 别名]
 def _findMatchUrl(self, tag):
     h3 = tag.find('h3')
     if not h3:
         return ''
     a = h3.find('a')
     url = a.attrs.get('href', '')
     # decode url
     host = parse.urlsplit(url).netloc
     path = url[len(parse.urljoin(url, '/')) - 1:]
     conn = HTTPConnection(host, timeout=10)
     conn.request('GET', path)
     req = conn.getresponse()
     r_url = req.getheader('Location')
     conn.close()
     return r_url
开发者ID:liuyug,项目名称:WebSpider,代码行数:17,代码来源:baidu.py

示例6: test_garbage_in

# 需要导入模块: from six.moves.http_client import HTTPConnection [as 别名]
# 或者: from six.moves.http_client.HTTPConnection import close [as 别名]
 def test_garbage_in(self):
     # Connect without SSL regardless of server.scheme
     c = HTTPConnection('%s:%s' % (self.interface(), self.PORT))
     c._output(b'gjkgjklsgjklsgjkljklsg')
     c._send_output()
     response = c.response_class(c.sock, method='GET')
     try:
         response.begin()
         self.assertEqual(response.status, 400)
         self.assertEqual(response.fp.read(22),
                          b'Malformed Request-Line')
         c.close()
     except socket.error:
         e = sys.exc_info()[1]
         # "Connection reset by peer" is also acceptable.
         if e.errno != errno.ECONNRESET:
             raise
开发者ID:Southpaw-TACTIC,项目名称:TACTIC,代码行数:19,代码来源:test_http.py

示例7: _get_version

# 需要导入模块: from six.moves.http_client import HTTPConnection [as 别名]
# 或者: from six.moves.http_client.HTTPConnection import close [as 别名]
    def _get_version(self, master):
        if master is not None:
            conn = None
            host, port = master.split(':', 2)
            port = int(port)
            try:
                conn = HTTPConnection(host, port, timeout=self._timeout)
                conn.request('GET', '/version')
                resp = conn.getresponse()
                if resp.status < 200 or resp.status >= 300:
                    return

                return json.loads(resp.read().decode('utf-8'))['version']
            except Exception:
                logger.exception('Error')
                pass
            finally:
                if conn:
                    conn.close()
开发者ID:douban,项目名称:pymesos,代码行数:21,代码来源:scheduler.py

示例8: MesosExecutorDriver

# 需要导入模块: from six.moves.http_client import HTTPConnection [as 别名]
# 或者: from six.moves.http_client.HTTPConnection import close [as 别名]
class MesosExecutorDriver(Process, ExecutorDriver):
    _timeout = 10

    def __init__(self, executor, use_addict=False):
        env = os.environ
        agent_endpoint = env['MESOS_AGENT_ENDPOINT']
        super(MesosExecutorDriver, self).__init__(master=agent_endpoint)

        framework_id = env['MESOS_FRAMEWORK_ID']
        assert framework_id
        self.framework_id = dict(value=framework_id)
        executor_id = env['MESOS_EXECUTOR_ID']
        self.executor_id = dict(value=executor_id)
        grace_shutdown_period = env.get('MESOS_EXECUTOR_SHUTDOWN_GRACE_PERIOD')
        if grace_shutdown_period:
            self.grace_shutdown_period = parse_duration(grace_shutdown_period)
        else:
            self.grace_shutdown_period = 0.0

        self.checkpoint = bool(env.get('MESOS_CHECKPOINT'))
        self.local = bool(env.get('MESOS_LOCAL'))

        self.executor = executor
        self.framework_info = None
        self.executor_info = None
        self.tasks = {}
        self.updates = {}
        self._conn = None
        self._dict_cls = Dict if use_addict else dict

    def _delay_kill(self):
        def _():
            try:
                time.sleep(self.grace_shutdown_period)
                os.killpg(0, signal.SIGKILL)
            except Exception:
                logger.exception('Failed to force kill executor')

        t = Thread(target=_)
        t.daemon = True
        t.start()

    def gen_request(self):
        body = json.dumps(dict(
            type='SUBSCRIBE',
            framework_id=self.framework_id,
            executor_id=self.executor_id,
            subscribe=dict(
                unacknowledged_tasks=list(self.tasks.values()),
                unacknowledged_updates=list(self.updates.values()),
            ),
        ))

        request = ('POST /api/v1/executor HTTP/1.1\r\nHost: %s\r\n'
                   'Content-Type: application/json\r\n'
                   'Accept: application/json\r\n'
                   'Connection: close\r\nContent-Length: %s\r\n\r\n%s') % (
                       self.master, len(body), body
        )
        return request.encode('utf-8')

    def on_close(self):
        if self._conn is not None:
            self._conn.close()
            self._conn = None
            self.version = None

        self.executor.disconnected(self)
        if not self.checkpoint:
            if not self.local:
                self._delay_kill()
            self.executor.shutdown(self)
            self.abort()

    def on_event(self, event):
        if 'type' in event:
            _type = event['type'].lower()
            if _type == 'shutdown':
                self.on_shutdown()
                return

            if _type == 'heartbeat':
                return

            if _type not in event:
                logger.error(
                    'Missing `%s` in event %s' %
                    (_type, event))
                return

            event = event[_type]
            func_name = 'on_%s' % (_type,)
            func = getattr(self, func_name, None)
            if func is not None:
                func(event)
            else:
                logger.error('Unknown type:%s, event:%s' % (_type, event))
        else:
            logger.error('Unknown event:%s' % (event,))

#.........这里部分代码省略.........
开发者ID:windreamer,项目名称:pymesos,代码行数:103,代码来源:executor.py

示例9: MesosSchedulerDriver

# 需要导入模块: from six.moves.http_client import HTTPConnection [as 别名]
# 或者: from six.moves.http_client.HTTPConnection import close [as 别名]
class MesosSchedulerDriver(Process, SchedulerDriver):
    _timeout = 10

    def __init__(self, sched, framework, master_uri,
                 use_addict=False, implicit_acknowledgements=True,
                 principal=None, secret=None, failover=False,
                 timeout=DAY):
        super(MesosSchedulerDriver, self).__init__(timeout=timeout)
        self.sched = sched
        self.master_uri = master_uri
        self._framework = framework
        self.detector = None
        self._conn = None
        self.version = None
        self._failover = failover
        self._dict_cls = Dict if use_addict else dict
        self.implicit_acknowledgements = implicit_acknowledgements
        if principal is not None and secret is not None:
            self._basic_credential = 'Basic %s' % (
                b2a_base64(
                    ('%s:%s' % (principal, secret)).encode('ascii')
                ).decode('ascii').strip()
            )
        else:
            self._basic_credential = None

    @property
    def framework(self):
        framework = dict(self._framework)
        version = self.version and tuple(
            int(n) for n in self.version.split('.')
        )

        capabilities = [
            c for c in framework.get('capabilities', [])
            if c['type'] != 'GPU_RESOURCES'
        ]

        if version and version >= (1, 0, 0):
            capabilities.append(dict(type='GPU_RESOURCES'))

        if capabilities:
            framework['capabilities'] = capabilities
        else:
            framework.pop('capabilities', None)

        if 'failover_timeout' not in framework:
            framework['failover_timeout'] = 100

        return framework

    @property
    def framework_id(self):
        id = self._framework.get('id')
        return id and id.get('value')

    @framework_id.setter
    def framework_id(self, id):
        self._framework['id'] = dict(value=id)

    def _get_version(self, master):
        if master is not None:
            conn = None
            host, port = master.split(':', 2)
            port = int(port)
            try:
                conn = HTTPConnection(host, port, timeout=self._timeout)
                conn.request('GET', '/version')
                resp = conn.getresponse()
                if resp.status < 200 or resp.status >= 300:
                    return

                return json.loads(resp.read().decode('utf-8'))['version']
            except Exception:
                logger.exception('Error')
                pass
            finally:
                if conn:
                    conn.close()

    def change_master(self, master):
        self.version = self._get_version(master)
        super(MesosSchedulerDriver, self).change_master(master)
        self._close()

    def start(self):
        super(MesosSchedulerDriver, self).start()
        uri = self.master_uri
        if uri.startswith('zk://') or uri.startswith('zoo://'):
            from .detector import MasterDetector
            self.detector = MasterDetector(uri[uri.index('://') + 3:], self)
            self.detector.start()
        else:
            if ':' not in uri:
                uri += ':5050'
            self.change_master(uri)

    def stop(self, failover=False):
        with self._lock:
            self._failover = failover
#.........这里部分代码省略.........
开发者ID:douban,项目名称:pymesos,代码行数:103,代码来源:scheduler.py

示例10: MesosOperatorDaemonDriver

# 需要导入模块: from six.moves.http_client import HTTPConnection [as 别名]
# 或者: from six.moves.http_client.HTTPConnection import close [as 别名]
class MesosOperatorDaemonDriver(OperatorDaemonDriver):
    _timeout = 10

    def __init__(self, daemon_uri):
        self.init(daemon_uri)

    def init(self, daemon_uri):
        """
        :param daemon_uri: masterHost:5050 or agentHost:5051
        """
        self._daemon = daemon_uri
        self._conn = None

    def _get_conn(self):
        if self._conn is not None:
            return self._conn

        host, port = self._daemon.split(':', 2)
        port = int(port)
        self._conn = HTTPConnection(host, port, timeout=self._timeout)
        return self._conn

    def _send(self, body, path='/api/v1/operator', method='POST', headers={}):
        with self._lock:
            conn = self._get_conn()
            if conn is None:
                raise RuntimeError('Not connected yet')

            if body != '':
                data = json.dumps(body).encode('utf-8')
                headers['Content-Type'] = 'application/json'
            else:
                data = ''

            # stream_id = self.stream_id
            # if stream_id:
            #     headers['Mesos-Stream-Id'] = stream_id

            try:
                conn.request(method, path, body=data, headers=headers)
                resp = conn.getresponse()
            except Exception:
                self._conn.close()
                self._conn = None
                raise

            if resp.status >= 300 and resp.status <= 399:
                url = resp.getheader('location')
                parsed = urlparse(url)
                self._daemon = '%s:%s' % (parsed.hostname, parsed.port)
                self._conn.close()
                self._conn = None
                return self._send(body, path, method, headers)

            if resp.status < 200 or resp.status >= 300:
                raise RuntimeError(
                    'Failed to send request code=%s, message=%s' % (
                        resp.status, resp.read()
                    )
                )

            result = resp.read()
            if not result:
                return {}

            try:
                return json.loads(result.decode('utf-8'))
            except Exception:
                return {}

    def getHealth(self):
        body = dict(
            type='GET_HEALTH',
        )
        return self._send(body)

    def getFlags(self):
        body = dict(
            type='GET_FLAGS',
        )
        return self._send(body)

    def getVersion(self):
        body = dict(
            type='GET_VERSION',
        )
        return self._send(body)

    def getMetrics(self, timeout):
        body = dict(
            type='GET_METRICS',
            get_metrics=dict(
                timeout=dict(
                    nanoseconds=timeout,
                )
            )
        )
        return self._send(body)

    def getLoggingLevel(self):
#.........这里部分代码省略.........
开发者ID:douban,项目名称:pymesos,代码行数:103,代码来源:operator_v1.py


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