【python + NATAPP】实现内网穿透的简易数据传输
1. 服务端
接收两张图像的地址,返回这两张图像的相似度
import os, shutil, requests
import cv2
import numpy as np
import imgs_similari_api
from flask import Flask,render_template,request
def cleanfiles(temproot):
if os.path.exists(temproot):
shutil.rmtree(temproot)
os.makedirs(temproot)
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/imgsimilar",methods = ['GET', 'POST'])
def login():
if request.method == "POST":
imgurl1 = request.form.get('imgurl1')
imgurl2 = request.form.get('imgurl2')
print(imgurl1)
print(imgurl2)
print('downing ... ...')
r = requests.get(imgurl1)
r.raise_for_status()
# clean files
cleanfiles('./tempimgs')
with open('./tempimgs/abc1.jpg','wb') as f:
f.write(r.content)
f.close()
r = requests.get(imgurl2)
r.raise_for_status()
with open('./tempimgs/abc2.jpg','wb') as f:
f.write(r.content)
f.close()
image1 = cv2.imread('./tempimgs/abc1.jpg')
image2 = cv2.imread('./tempimgs/abc2.jpg')
print('computing ... ...')
value_img_similar = imgs_similari_api.classify_hist_with_split(image1, image2)
return {'sorce':str(value_img_similar)}
else:
return "<h1>login Failure !</h1>"
if __name__ == '__main__':
host = '127.0.0.1'
port = '8888'
app.run(debug=True, host=host, port=port)
2. 内网穿透
使用NATAPP的免费服务,先去注册,在依次进入“我的渠道”>>“购买渠道”>>“免费渠道”,设置一下端口就可以购买了。
接下来下载客户端和配置config.ini,只需要修改authtoken,换成你自己的authtoken。
运行客户端
获得映射网址,如:http://aaa.natappfree.cc -> 127.0.0.1:8888(8888为购买时设置的端口)(注意:每次开启内网穿透服务,地址会变化!!!)
3. 客户端
#coding=utf-8
import requests
s = requests
data={'imgurl1':'https://ps.ssl.qhmsg.com/bdr/720__/t017843e759f2628d1f.jpg', 'imgurl2':'https://ps.ssl.qhmsg.com/bdr/720__/t017843e759f2628d1f.jpg'}
r = s.post('http://aaa.natappfree.cc/imgsimilar', data)
print(r.text)
4. 运行
实际的步骤是先搞定内网穿透,再写好服务端和客户端程序(本文的服务端程序缺少计算图像相似度算法不能运行,仅作示意)。
总结就是:开启内网穿透服务>>开启服务端服务>>客户端调用
现在全球各地都可以通过这个方式调用你写好的功能了!!!

浙公网安备 33010602011771号