pytho3 urllib urllib3 获取本地ip地址和mac地址发送post到指定http服务器




import time
import socket
import uuid
import urllib
import urllib3
from urllib import request, parse

import json
def get_host_ip():
    """
    :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


def get_mac_address():
    mac=uuid.UUID(int = uuid.getnode()).hex[-12:]
    return ":".join([mac[e:e+2] for e in range(0,11,2)])
def send_post(url):
    ip = get_host_ip()
    print(ip)
    mac = get_mac_address()
    parms = {"ip":ip,"mac":mac}
    querystring = parse.urlencode(parms)
    u = request.urlopen(url, querystring.encode('utf-8'))
    resp = u.read()


if __name__ == '__main__':
    url = "http://simp00.top:9823/recv/"
    while True:
        try:
            send_post(url)
            time.sleep(20)
        except Exception as e:
            print(e)



posted @ 2022-03-07 20:06  simp00  阅读(199)  评论(0编辑  收藏  举报