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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (362)

运行High Sierra时,我在osx上遇到了麻烦。

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

这个代码在linux上运行正常。我正在研究如何在Windows上尝试它,并希望另外安装osx。

我在这里完全失败了。事实上它甚至没有引起错误对我来说很奇怪。

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()

此外,如果我使用调试器,它会在请求库中的此处进入黑洞。

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

此代码位于requests / utils.py中

提问于
用户回答回答于

似乎requests在MacOS。尝试通过设置trust_envFalse:禁用代理:

session = requests.Session()
session.trust_env = False  # No proxy settings from the OS
r = session.get(url)

扫码关注云+社区

领取腾讯云代金券