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



import time
import socket
import uuid
import urllib
import urllib2
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()
    req = urllib2.Request(url)
    data = urllib.urlencode({"ip":ip,"mac":mac})
    resp = urllib2.urlopen(req,data)  # post


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-03 19:39  simp00  阅读(95)  评论(0编辑  收藏  举报