首页 > 代码库 > python 之返回本机外网IP
python 之返回本机外网IP
server 端
#!/usr/local/anaconda3/bin/python import socket import threading # ==================================================== # Author: chang - EMail:changbo@hmg100.com # Last modified: 2017-06-20 # Filename: sendoutip.py # Description: send u out ip ,base socket # blog:http://www.cnblogs.com/changbo # ==================================================== port = 8899 host = ‘x.x.x.xxx‘ def sendOut(): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((host, port)) s.listen(5) while True: connection, address = s.accept() ip, _ = address ip = (str(ip)).encode(‘utf-8‘) while True: data = connection.recv(1024) if not data: break connection.send(ip) # s.close() connection.close() s.close() if __name__ == ‘__main__‘: t = threading.Thread(target=sendOut) t.start()
client 端
#!/usr/local/anaconda3/bin/python import socket import struct # ==================================================== # Author: chang - EMail:changbo@hmg100.com # Last modified: 2017-06-20 # Filename: sendoutip.py # Description: send u out ip ,base socket # blog:http://www.cnblogs.com/changbo # ==================================================== port = 8899 host = ‘x.x.x.x‘ def getOut(): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) data = ‘hi‘ s.send(str(data).encode(‘utf-8‘)) results = s.recv(1024) print(str(results, ‘utf-8‘)) s.close() if __name__ == ‘__main__‘: getOut()
END!
python 之返回本机外网IP
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。