2016-10-05 2 views
2

파이썬 요청 모듈을 사용하여 다음 코드를 사용하여 서버에 값을 전달하지는 않지만 http 요청에서 페이로드 값의 순서를 변경하려고합니다.정렬없이 파이썬 요청 페이로드

payload3 = {'OSILA_LinkTo_Site': OSILA_LinkTo_Site, 'OSILA_LinkTo_Service': OSILA_LinkTo_Service, 'OSILA_Locale': OSILA_Locale, 'OSILA_Locale_display': OSILA_Locale, 'OSILA_TimeZone': OSILA_TimeZone, 'OSILA_TimeZone_display': OSILA_TimeZone, 'selectedUser': '', 'OSILA_LinkTo_User_display': '', 'OSILA_LinkTo_User': OSILA_LinkTo_User, 'OSILA_DisplayName': OSILA_DisplayName, 'OSILA_CountryCode': OSILA_CountryCode, 'OSILA_AreaCode': OSILA_AreaCode, 'OSILA_LocalOfficeCode': OSILA_LocalOfficeCode, 'OSILA_LinkTo_Extension': OSILA_LinkTo_Extension, 'OSILA_ProductPackage': OSILA_ProductPackage, 'OSILA_ProductPackage_display': OSILA_ProductPackage, 'OSILA_DeviceType': OSILA_DeviceType, 'OSILA_DeviceType_display': OSILA_DeviceType, 'OSILA_Parameter_4': OSILA_Parameter_4, 'OSILA_Parameter_4_display': OSILA_Parameter_4, 'OSILA_Parameter_3': OSILA_Parameter_3, 'OSILA_Parameter_3_display': OSILA_Parameter_3, 'OSILA_Parameter_2': OSILA_Parameter_2, 'OSILA_Parameter_2_display': OSILA_Parameter_2, 'OSILA_Parameter_5': OSILA_Parameter_5, 'OSILA_Parameter_5_display': OSILA_Parameter_5, 'OSILA_Parameter_1': OSILA_Parameter_1, 'OSILA_Parameter_1_display': OSILA_Parameter_1, 'OSILA_CreateNewFax': OSILA_CreateNewFax, 'OSILA_LinkTo_ServiceFax': '', 'OSILA_CountryCodeFax': '', 'OSILA_AreaCodeFax': '', 'OSILA_LocalOfficeCodeFax': '', 'OSILA_LinkTo_ExtensionFax': '', 'OSILA_ProvisioningDate': OSILA_ProvisioningDate, 'OSILA_DateMail_1': OSILA_ProvisioningDate, 'OSILA_DateMail_2': '', 'OSILA_DateMail_3': '', 'OSILA_DateMail_4': '', 'OSILA_DateMail_5': '', 'OSILA_DateMail_6': '', 'OSILA_DateMail_7': '', 'OSILA_DateMail_8': '', 'OSILA_DateMail_9': '', 'attachEventListeners': '', 'webMgrRequestId': webMgrRequestId, 'WT': WT2, 'enterConfirm': 'true', 'users_R_0_C__select': 'on', 'selectedUser': '0'} 

subcr = session.post("http://192.168.0.10:8080/OSILAManager/createSubscriber2.do", data=payload3)

나는 값이 다음의 순서로 전달되어 볼 수있는 브라우저를 사용하지만 그것의 순서를 변경 Python을 사용할 때,이 분류

OSILA_LinkTo_Site 
OSILA_LinkTo_Service 
OSILA_Locale 
OSILA_Locale_display 
OSILA_TimeZone 
OSILA_TimeZone_display 
users_R_0_C__select 
selectedUser 
OSILA_LinkTo_User_display 
OSILA_LinkTo_User 
OSILA_DisplayName 
OSILA_CountryCode 
OSILA_AreaCode 
OSILA_LocalOfficeCode 
OSILA_LinkTo_Extension 
OSILA_ProductPackage 
OSILA_ProductPackage_display 
OSILA_DeviceType 
OSILA_DeviceType_display 
OSILA_Parameter_4 
OSILA_Parameter_4_display 
OSILA_Parameter_3 
OSILA_Parameter_3_display 
OSILA_Parameter_2 
OSILA_Parameter_2_display 
OSILA_Parameter_5 
OSILA_Parameter_5_display 
OSILA_Parameter_1 
OSILA_Parameter_1_display 
OSILA_CreateNewFax 
OSILA_LinkTo_ServiceFax 
OSILA_CountryCodeFax 
OSILA_AreaCodeFax 
OSILA_LocalOfficeCodeFax 
OSILA_LinkTo_ExtensionFax 
OSILA_ProvisioningDate 
OSILA_DateMail_1 
OSILA_DateMail_2 
OSILA_DateMail_3 
OSILA_DateMail_4 
OSILA_DateMail_5 
OSILA_DateMail_6 
OSILA_DateMail_7 
OSILA_DateMail_8 
OSILA_DateMail_9 
attachEventListeners 
webMgrRequestId 
WT 
enterConfirm 
+0

사전에는 순서가 없습니다. 'collections.OrderedDict'를 사용하십시오 –

+1

또는 파이썬 3.6 - 그들은 파이썬 3.6으로 주문했습니다. – Mathias

+0

중복 키가 있으므로 아마도 더 큰 문제 일 수 있습니다. –

답변

1
을 피할 수있는 방법이 존재

dict 대신에 튜플을 전달할 수 있습니다.

In [7]: 
    ...: import requests 
    ...: d = {"foo":"bar","bar":"foo","123":"456"} 
    ...: t = (("foo","bar"),("bar","foo"),("123","456")) 
    ...: r1 = requests.post("http://httpbin.org", data=d) 
    ...: r2 = requests.post("http://httpbin.org", data=t) 
    ...: print(r1.request.body) 
    ...: print(r2.request.body) 
    ...: 
bar=foo&123=456&foo=bar 
foo=bar&bar=foo&123=456 

또한 y 두 번째 dict에 두 번 있습니다. 마지막 키/값 쌍만 지정 했으므로 올바른 형식이 아닌 터플로 다시 키를 반복 할 수 있으므로 작동하지 않습니다.

In [8]: import requests 
    ...: d = {"foo":"bar","bar":"foo","123":"456","selectedUser":"0", "selectedUs 
    ...: er":"1"} 
    ...: t = (("foo","bar"),("bar","foo"),("123","456"), ("selectedUser","0"),("s 
    ...: electedUser","1")) 
    ...: r1 = requests.post("http://httpbin.org", data=d) 
    ...: r2 = requests.post("http://httpbin.org", data=t) 
    ...: print(r1.request.body) 
    ...: print(r2.request.body) 
    ...: 
bar=foo&selectedUser=1&123=456&foo=bar 
foo=bar&bar=foo&123=456&selectedUser=0&selectedUser=1 

dict을 사용하면 할당 된 마지막 쌍인 selectedUser=1으로 끝납니다. 그게 실제로 게시물 신체 키의 순서보다 더 문제가 될 수 있습니다. selectedUser을 사용하기 만하면 게시하려는 의도가 아니던 부분을 제거하십시오.