网卡别名或网卡绑定任意IP地址

2018-05-08 09:21  242人阅读  评论 (0)
Tags: network

Linux网卡别名

sudo ifconfig lo0 inet 127.0.0.2 alias
sudo ifconfig lo0 inet 127.0.0.2 delete

Windows网卡别名

netsh interface ip add address "Local Area Connection" 33.33.33.33 255.255.255.255

Windows网卡属性配置

网卡属性

Server测试代码

import socket

def main():
    serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    serv.bind(("", 8080))
    # serv.bind(("0.0.0.0", 8080))
    serv.listen(128)

    while True:
        print("-" * 20)
        sock, addr = serv.accept()
        print(serv.getsockname())
        print(sock.getsockname())
        print(addr)
        sock.close()

if __name__ == '__main__':
    main()

Client测试代码

import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# sock.bind(("127.0.0.2", 12345))
sock.connect(("127.0.0.2", 8080))
sock.close()