Raspberry Pi python gets its own IP

 

 

 

 

 

Guess the second one should be the Raspberry Pi's own IP when establishing WIFI

The first is the IP assigned when the Raspberry Pi connects to the Internet

import socket
def get_host_ip():
    """
    Query local ip address
    :return: ip
    """
    try:
        s = socket.socket (socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(('8.8.8.8', 80))
        ip = s.getsockname()[0]
    finally:
        s.close()
    return ip
 
if __name__ == '__main__':
    #the first method
    print(get_host_ip())
    
    #The second method
    # Get the local computer name
    hostname = socket.gethostname()
    # Get local ip
    ip = socket.gethostbyname(hostname)
    print(ip)

  

Identify and display

 

 

#!/usr/bin/env python
#-*- coding: UTF-8 -*-
import os, signal, subprocess
import cv2

strfile1 = "qrcode.png"

import socket
def get_host_ip():
    """
    Query local ip address
    :return: ip
    """
    try:
        s = socket.socket (socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(('8.8.8.8', 80))
        ip = s.getsockname()[0]
    finally:
        s.close()
    return ip

if __name__ == '__main__':
    #the first method
    IP='http://'+get_host_ip()+":8080"
    print(IP)
    os.system ("qrencode -o" + strfile1 + "'" + IP + "'") # Save the generated QR code
    
    img = cv2.imread (strfile1) # Load the generated QR code image
    cv2.namedWindow("cs",0)
    cv2.imshow ("cs", img) #Show it
    cv2.waitKey(1000)

  

Guess you like

Origin www.cnblogs.com/kekeoutlook/p/12723833.html