首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Requests.get未完成,不会引发任何错误

Requests.get未完成,不会引发任何错误
EN

Stack Overflow用户
提问于 2019-03-29 06:53:34
回答 2查看 118关注 0票数 2

我在osx上遇到了麻烦,特别是在运行High Sierra。

问题是在下面的代码中,对requests.get的调用没有做任何事情。它根本不会超出调用范围,也不会引发错误。get调用后的print方法不会执行,但应用程序不会崩溃,它只是继续执行主循环,就像什么都没发生一样。

同样的代码在linux上运行得很好。我正在研究如何在windows上试用它,希望也能安装另一个osx。

我在这里完全不知所措。事实上,它甚至没有引发错误,这对我来说很奇怪。

代码语言:javascript
复制
import tkinter as tk
from tkinter import ttk
import multiprocessing
import time
import requests


class SplashScreen(tk.Toplevel):

    def __init__(self, root):
        super(SplashScreen, self).__init__(root)

        self.width = 800
        self.height = 400
        self.geometry('{Width}x{Height}'.format(Width=self.width, Height=self.height))
        self.overrideredirect(True)

        self.label = ttk.Label(self, text='My Splash', anchor='center')
        self.label.grid(column=0, row=2)

    def destroy_splash_screen(self):
        self.destroy()
        print('destroyed splash')


class App(tk.Tk):

    def __init__(self):
        super(App, self).__init__()

        self.splash = SplashScreen(self)

        self.withdraw()

        process_startup = multiprocessing.Process(
            target=self.startup_process,
            args=("stuff",)
        )
        process_startup.start()
        self.splash.update()

        while process_startup.is_alive():
            time.sleep(0.1)

        self.title("MyApp")

        self.mainloop()

    def mainloop(self, n=0):
        first_loop = True
        while True:
            self.update_idletasks()
            self.update()
            if first_loop:
                self.remove_splash_screen()
                first_loop = False

    def startup_process(self, things):
        init_client()

    def remove_splash_screen(self):
        self.splash.destroy_splash_screen()
        del self.splash
        self.deiconify()

def init_client():
    requests.get("http://httpbin.org/ip")
    s = 'strfff'
    print(s)


if __name__ == '__main__':
    app = App()

另外,如果我继续使用调试器,它会在请求库的这一点上进入黑洞。

代码语言:javascript
复制
def should_bypass_proxies(url, no_proxy):
    """
    Returns whether we should bypass proxies or not.

    :rtype: bool
    """
    # Prioritize lowercase environment variables over uppercase
    # to keep a consistent behaviour with other http projects (curl, wget).
    get_proxy = lambda k: os.environ.get(k) or os.environ.get(k.upper())

    # First check whether no_proxy is defined. If it is, check that the URL
    # we're getting isn't in the no_proxy list.
    no_proxy_arg = no_proxy
    if no_proxy is None:
        no_proxy = get_proxy('no_proxy')
    parsed = urlparse(url)

    if parsed.hostname is None:
        # URLs don't always have hostnames, e.g. file:/// urls.
        return True

    if no_proxy:
        # We need to check whether we match here. We need to see if we match
        # the end of the hostname, both with and without the port.
        no_proxy = (
            host for host in no_proxy.replace(' ', '').split(',') if host
        )

        if is_ipv4_address(parsed.hostname):
            for proxy_ip in no_proxy:
                if is_valid_cidr(proxy_ip):
                    if address_in_network(parsed.hostname, proxy_ip):
                        return True
                elif parsed.hostname == proxy_ip:
                    # If no_proxy ip was defined in plain IP notation instead of cidr notation &
                    # matches the IP of the index
                    return True
        else:
            host_with_port = parsed.hostname
            if parsed.port:
                host_with_port += ':{}'.format(parsed.port)

            for host in no_proxy:
                if parsed.hostname.endswith(host) or host_with_port.endswith(host):
                    # The URL does match something in no_proxy, so we don't want
                    # to apply the proxies on this URL.
                    return True

    with set_environ('no_proxy', no_proxy_arg):
        # parsed.hostname can be `None` in cases such as a file URI.
        try:
    # It executes this method and returns when stepped through but from here it just ends. doesn't go anywhere from this point
            bypass = proxy_bypass(parsed.hostname)
        except (TypeError, socket.gaierror):
            bypass = False

    if bypass:
        return True

    return False

此代码位于request/utils.py中

任何关于从哪里开始的想法。就像我说的,我现在完全不知所措。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-29 07:20:24

看起来MacOS上的requests有点buggy。尝试通过将trust_env设置为False来禁用代理

代码语言:javascript
复制
session = requests.Session()
session.trust_env = False  # No proxy settings from the OS
r = session.get(url)
票数 1
EN

Stack Overflow用户

发布于 2021-10-24 16:39:27

请检查以下内容:

代码语言:javascript
复制
from sys import platform
if platform == "darwin":
    os.environ['no_proxy'] = '*'

在我的例子中,这个解决方案适用于MacOSx。

参考:requests: how to disable / bypass proxy

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55408047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档