Giter Club home page Giter Club logo

Comments (2)

Wangler2333 avatar Wangler2333 commented on August 17, 2024

@PengYunjing 感谢您的建议,针对获取ip方法click_get_ip()已修改为如下形式:

def click_get_ip(self):
    """
    pushbutton_get_ip控件点击触发的槽
    :return: None
    """
    # 获取本机ip
    self.lineEdit_ip_local.clear()
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        s.connect(('8.8.8.8', 80))
        my_addr = s.getsockname()[0]
        self.lineEdit_ip_local.setText(str(my_addr))
    except Exception as ret:
        # 若无法连接互联网使用,会调用以下方法
        # self.signal_write_msg.emit("无法获取ip,请连接网络!\n")
        my_addr = socket.gethostbyname(socket.gethostname())
        self.lineEdit_ip_local.setText(str(my_addr))
    finally:
        s.close()

from tcp_udp_web_tools-pyqt5.

Wangler2333 avatar Wangler2333 commented on August 17, 2024

@PengYunjing 刚刚的修改未考虑到macOS获取ip会报错这个问题,以下为最终代码:

def click_get_ip(self):
    """
    pushbutton_get_ip控件点击触发的槽
    :return: None
    """
    # 获取本机ip
    self.lineEdit_ip_local.clear()
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        s.connect(('8.8.8.8', 80))
        my_addr = s.getsockname()[0]
        self.lineEdit_ip_local.setText(str(my_addr))
    except Exception as ret:
        # 若无法连接互联网使用,会调用以下方法
        try:
            my_addr = socket.gethostbyname(socket.gethostname())
            self.lineEdit_ip_local.setText(str(my_addr))
        except Exception as ret_e:
            self.signal_write_msg.emit("无法获取ip,请连接网络!\n")
    finally:
        s.close()

from tcp_udp_web_tools-pyqt5.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.