Python之获取当前IP

Python之获取当前IP

import socket


def get_host_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__':
    print(get_host_ip())

获取请求可以通过request.remote_addr直接获取ip;response.status_code获取访问状态;request.url获取访问url

你可能感兴趣的:(Python之获取当前IP)