3
我想轉換原始curl命令使用Python請求模塊,但沒有運氣。這是一個查詢JBoss Mgmt接口的簡單請求,但它不能正確解析我的JSON。Python請求模塊 - POST失敗 - 無效字符'o'
16:34:26,868 DEBUG [org.jboss.as.domain.http.api] (HttpManagementService-threads - 15) Unable to construct ModelNode 'Invalid character: o'
Python版本
Python 2.7.6
工作原cURL命令:
/usr/bin/curl --digest -v -L -D - 'http://brenn:[email protected]:9990/management' --header Content-Type:application/json '-d {"operation":"read-attribute","name":"server-state","json.pretty":1}'
在Python代碼我在REST讀/捲曲有效載荷像這樣
import requests
----
def readconfigfile():
with open('jboss_modification.cfg') as f:
lines = f.readlines()
return lines
的配置文件看起來像這樣
{"operation":"read-attribute","name":"server-state","json.pretty":1}
我轉換STR格式從readconfigfile()到詞典如下
def converttodictionary(incominglines):
commands = []
for lin in incominglines:
#dumps = json.dumps(lin)
obj = json.loads(lin)
commands.append(obj)
return commands
的Python代碼以執行該請求是如下
def applyconfig(lines):
url="http://localhost:9990/management"
auth=HTTPBasicAuth('brenn', '!12rori')
s = requests.Session()
re=s.get(url, auth=HTTPDigestAuth('brenn', '!12rori')) ##200 RESP
s.headers.update({'Content-Type': 'application/json'})
for line in lines:
payload=line
r=s.post(url,payload)
print(r.text)
任何幫助非常讚賞?
注:這個問題已經被幾次更新我解決其他問題....