簡體   English   中英

使用二進制數據而不是 XML - Python 接收 POST 文本/xml 請求

[英]Receiving POST text/xml request with binary data instead of XML - Python

我訂閱了每分鍾發送 2 個 POST 請求的數據服務。

內容類型是文本/XML:

{'Host': 'my.host.com', 'X-Real-Ip': '111.1.181.11', 'X-Forwarded-For': '111.1.181.11', 'X-Forwarded-Proto': 'https', 'Connection': 'close', 'Content-Length': '8556', 'Content-Type': 'text/xml', 'Accept-Encoding': 'identity,gzip', 'Content-Encoding': 'gzip', 'Soapaction': '"http://datex2.eu/wsdl/supplierPush/2_0/putDatex2Data"', 'User-Agent': 'Jakarta Commons-HttpClient/3.0'}

我將請求保存到文件中,以便可以使用以下 function 檢查其內容:

def receive_post_request_and_save_it_to_file(request):

    if request.method == "GET":
        return redirect("/")

    elif request.method == "POST":
        if request.content_type == "text/xml" and request.body:
            try:
                filename = "my-request.xml"
                file_path = "request-files/"
                if not os.path.exists(path):
                    os.makedirs(path)
                with open(os.path.join(path, filename), 'wb') as f:
                    f.write(data_to_process)
                return HttpResponse(status=200)

            except Exception as e:
                print("Something went wrong => {}".format(e))
                return HttpResponse(status=500)
        
    else:
        return HttpResponse(status=404)

我沒有像以前從其他提供商那里收到 XML 文件,而是收到無法打開的二進制代碼:

��]�n�8�}�WyTLRԅ��T��Q@.�$�:g^����%C��J�̼�g�[��ٺ9�-鷺ɞmL�S1o"�6�E�\;\f�����Aϲ�|�����2���(o���x�������,��u�ix<
�R�g��(��yv����E�Y]⺰8*�7q�WM�C1�ǎ��~[&딏��'�N�4�����M��M�.�(����|��h���2��w��Z&o��ϳqTT���mC㻻���^�e�$�o9��Ƣ~���!;��:[Iz�mC����4J�5`�Z,f��?�:-�IR?|��UZ�O�X����0�"�Y�X�߼��77އÞ�p����s��,��/��Y]�Q<-��㛤XUa����r�;�54e�~���H0��0���B���[����8�N��{���rs
�+{�6f���4�*Wr�$|�x���?�$m�d�mڦ:m�MNu��$����N����,)�Fiv��E����ɛ�a������r������p��u���8��Λ�E�:�f��x���.�4��sx�q���������z�m������
yv�%%�W���x��F��X�E�����}\����@�I����ӯMx�����í��m�N��J�g�2/^j�N�6��4��o=y۔�u�����h��C����:�2�I�A/�y���C�^6�v��+��C�,�����M��1�W������|�%�[̾���~\��RWY�͒tz<J��t<��tPd�d/�����4O�9���(��E
��

我應該收到的 XML 文件如下所示:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><d2LogicalModel xmlns="http://datex2.eu/schema/2/2_0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" modelBaseVersion="2" xsi:schemaLocation="http://datex2.eu/schema/2/2_0 http://sdbby.heuboe.de:11447/d2Schema/StrategicRouting.xsd"><exchange><supplierIdentification><country>de</country><nationalIdentifier>DE-BY-SDB</nationalIdentifier></supplierIdentification></exchange><payloadPublication xsi:type="SituationPublication" lang="de"><publicationTime>2020-07-22T16:21:00+02:00</publicationTime><publicationCreator><country>de</country><nationalIdentifier>DE-BY-SDB</nationalIdentifier></publicationCreator><situation id="S1595414940876" version="1"><situationVersionTime>2020-07-22T12:49:00+02:00</situationVersionTime><headerInformation><confidentiality>noRestriction</confidentiality><informationStatus>real</informationStatus></headerInformation><situationRecord xsi:type="GeneralNetworkManagement" id="R1595414940876" version="1"><situationRecordCreationTime>2020-07-22T12:49:00+02:00</situationRecordCreationTime><situationRecordVersionTime>2020-07-22T12:49:00+02:00</situationRecordVersionTime><probabilityOfOccurrence>certain</probabilityOfOccurrence><validity><validityStatus>definedByValidityTimeSpec</validityStatus><validityTimeSpecification><overallStartTime>2020-07-22T12:49:00+02:00</overallStartTime><overallEndTime>2020-07-22T16:23:00+02:00</overallEndTime></validityTimeSpecification></validity><cause xsi:type="NonManagedCause"><causeDescription><values><value lang="de">Optimierte Zielführung zu den Parkplätzen Messe</value><value lang="en">improved routing to parking area Munich Trade Fair</value></values></causeDescription><causeType>other</causeType></cause><groupOfLocations xsi:type="Area"><alertCArea><alertCLocationCountryCode>D</alertCLocationCountryCode><alertCLocationTableNumber>1</alertCLocationTableNumber><alertCLocationTableVersion>15.1</alertCLocationTableVersion><areaLocation><alertCLocationName><values><value lang="de">Großraum München</value></values></alertCLocationName><specificLocation>548</specificLocation></areaLocation></alertCArea></groupOfLocations><actionPlanIdentifier>S-14c</actionPlanIdentifier><operatorActionStatus>implemented</operatorActionStatus><complianceOption>advisory</complianceOption><generalNetworkManagementType>other</generalNetworkManagementType><generalNetworkManagementExtension><generalNetworkManagementExtended xsi:type="StrategicRouteManagement"><nameOfRouteManagement><values><value lang="de">A94 West - Riem 2 Parkhaus West</value>
                            </values>
                        </nameOfRouteManagement><triggerOrigin><location xsi:type="Point"><pointByCoordinates><pointCoordinates><latitude>48.137062</latitude><longitude>11.617391</longitude>
    </pointCoordinates>
</pointByCoordinates><pointExtension><openlrExtendedPoint><openlrPointLocationReference><openlrPointAlongLine><openlrSideOfRoad>right</openlrSideOfRoad><openlrOrientation>withLineDirection</openlrOrientation><openlrPositiveOffset>346</openlrPositiveOffset><openlrLocationReferencePoint><openlrCoordinate><latitude>48.13819</latitude><longitude>11.6146</longitude>
                    </openlrCoordinate><openlrLineAttributes><openlrFunctionalRoadClass>FRC2</openlrFunctionalRoadClass><openlrFormOfWay>singleCarriageway</openlrFormOfWay><openlrBearing>182</openlrBearing>
                    </openlrLineAttributes><openlrPathAttributes><openlrLowestFRCToNextLRPoint>FRC2</openlrLowestFRCToNextLRPoint><openlrDistanceToNextLRPoint>738</openlrDistanceToNextLRPoint>
                    </openlrPathAttributes>
                </openlrLocationReferencePoint><openlrLastLocationReferencePoint><openlrCoordinate><latitude>48.13779</latitude><longitude>11.62253</longitude>
                    </openlrCoordinate><openlrLineAttributes><openlrFunctionalRoadClass>FRC2</openlrFunctionalRoadClass><openlrFormOfWay>multipleCarriageway</openlrFormOfWay><openlrBearing>241</openlrBearing>
                    </openlrLineAttributes>
                </openlrLastLocationReferencePoint>
            </openlrPointAlongLine>
        </openlrPointLocationReference>
    </openlrExtendedPoint>
</pointExtension>
                            </location>
                        </triggerOrigin><route>

我沒有收到 XML 請求,而是收到一個二進制請求。

問:

有沒有不同的方法來解析這些數據?

數據提供者是否發送了錯誤的數據?

我希望有人能指導我正確的方向。

提前致謝!

如果您查看目標網站上的響應標頭,您將看到:

Content-Encoding: gzip

這意味着內容是使用 gzip 編碼的(請參閱https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding )。

在普通的 web 瀏覽器上,您永遠不會看到這一點,因為瀏覽器會自動進行解碼。

Python has a module called zlib with specific commands for decoding gzip encoding (see https://docs.python.org/2/library/gzip.html ).

一旦你解碼了數據,你應該能夠像普通的 XML 一樣處理它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM