为了方便开发同学,用Python写了一个POST请求发送可执行脚本,方便那些不能使用postman等使用的局域网。当然可以使用CURL命令、用PHP写一个也未尝不可,但并不具备跨平台性。
当然也可以使用go进行开发,可本人还不太会。废话不多说,show me code。
获取到 Linux 系统配置的 apollo 配置中心地址:http://apolloUrl/
通过访问apollo配置中心 http://apolloUrl/configs/公共配置ID/default/公共配置namespace,获取到eureka的服务器地址。
然后访问 http://eurekaUrl/eureka/apps/ 获取到注册应用的真实ip地址。
解析XML配置,最后替换并访问正确的目标地址。
import json import os import re import sys import requests from xml.dom.minidom import parseString from sys import argv # 公共配置appId PUBLIC_APOLLO_CONFIG_APP_ID = "xxxx.public" # 公共配置namepsace PUBLIC_APOLLO_CONFIG_NAMESPACE = "PUBLIC" # 公共配置key EUREKA_URL_KEY = "eureka.service.url" JSON_HEADERS = { ‘content-type‘: "application/json;charset=UTF-8", ‘user-agent‘: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36" } def doPost(url, postData): # 提交post请求 response = requests.post(url, headers=JSON_HEADERS, data=postData) return response def doGet(url): # 提交post请求 response = requests.get(url, headers=JSON_HEADERS) return response.text def isIpAddressPort(ipStr): p = re.compile(‘^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)(\:\d+)$‘) if p.match(ipStr): return True else: return False def parseEurekaUrl(confgJsonStr): data = json.loads(confgJsonStr) eurekaUrlStr = data[‘configurations‘][EUREKA_URL_KEY] eurekaUrls = eurekaUrlStr.split(‘,‘) return eurekaUrls[0] def getRealIpPort(configXml, appNamStr): # 读取文件 dom = parseString(configXml) # 获取文档元素对象 data = dom.documentElement # 获取 student applications = data.getElementsByTagName(‘application‘) for appInfo in applications: # 获取标签中内容 appName = appInfo.getElementsByTagName(‘name‘)[0].childNodes[0].nodeValue if appName != appNamStr: continue # 获取标签中内容 return appInfo.getElementsByTagName(‘instanceId‘)[0].childNodes[0].nodeValue def getRealHttpUrl(apolloUrl, httpUrl): httpUrlTmp = httpUrl.replace("http://", ‘‘) httpUrlTmp = httpUrlTmp.replace("https://", ‘‘) urls = httpUrlTmp.split("/") appName = urls[0].upper() if isIpAddressPort(appName): return httpUrl url = apolloUrl + "/configs/" + PUBLIC_APOLLO_CONFIG_APP_ID + "/default/" + PUBLIC_APOLLO_CONFIG_NAMESPACE eurekaUrl = parseEurekaUrl(doGet(url)) endStr = eurekaUrl[-1] if "/" == endStr: eurekaUrl = eurekaUrl[0:-1] eurekaUrl += "/apps" realIpPort = getRealIpPort(doGet(eurekaUrl), appName) return httpUrl.replace(appName, realIpPort) def callService(apolloUrl, httpUrl, postData): try: httpUrlOrigin = httpUrl httpUrl = getRealHttpUrl(apolloUrl=apolloUrl, httpUrl=httpUrl) print("\r\nRequestOriginUrl: {}".format(httpUrlOrigin)) print("RequestUrl: {}".format(httpUrl)) print("RequestMethod: POST") print("RequestData:") print("{}".format(json.dumps(json.loads(postData), indent=4))) response = json.loads(doPost(httpUrl, postData).text) print("ResponseDump:") print("{}\r\n".format(json.dumps(response, indent=4))) print("Response:") print("{}\r\n".format(json.dumps(response))) except Exception as e: print("error:{}".format(repr(e))) if __name__ == ‘__main__‘: apolloUrl = os.getenv(‘APOLLO_META‘) if 0 == len(apolloUrl): print("Please Set the APOLLO_META variable in the /etc/profile file !") sys.exit(1) if len(argv) < 3: print("Please input URL and PostData !") print( "Simple: ./springTest http://ABC/cde/fgh ‘{\"a\":\"b\"}‘ Or ./springTest http://ip[:port]/cde/fgh ‘{\"a\":\"b\"}‘") sys.exit(1) callService(apolloUrl, argv[1], argv[2])
pip3 install pyinstaller
pyinstaller -F springTest.py
./springTest http://应用id/xxxCtl/xxxAction ‘{"a":"b"}‘
./springTest http://ip[:port]/xxxCtl/xxxAction ‘{"a":"b"}‘
Shell简易版本,不支持SpringCloud:
#!/bin/bash URL=$1 DATA_JSON=$2 if [ "${URL}"x == ""x ];then echo "第一个参数为URL,第二个参数为json字符串"; echo "例如: ./[self].sh http://www.baidu.com ‘{\"a\":\"b\"}‘" exit 0 fi if [ "${DATA_JSON}"x == ""x ];then echo "第二个参数为json字符串不能为空"; exit 0 fi CURL_CMD="curl -H \"Content-Type:application/json\" -X POST --data ‘${DATA_JSON}‘ ${URL}" echo "" echo "${CURL_CMD}" echo "=========================================================================================" echo "" eval ${CURL_CMD} echo "" echo ""
PS:
编译时报依赖库不存在解决办法:http://www.pythonheidong.com/blog/article/158136/
一个简易的SpringCloud Http Post 请求发送工具
原文:https://www.cnblogs.com/phpdragon/p/12551992.html