Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python XML响应中的奇怪字符_Python_Xml_Sockets_Http_Post - Fatal编程技术网

Python XML响应中的奇怪字符

Python XML响应中的奇怪字符,python,xml,sockets,http,post,Python,Xml,Sockets,Http,Post,我尝试通过HTTP使用XML与我们的供应商通信 到目前为止,我的要求是: POST /some-service HTTP/1.1 Host: me Content-Type: text/xml; charset=utf-8 Content-Length: 470 <?xml version="1.0" ?> <PNARequest> <Version>2.0</Version> <TransactionHeader>

我尝试通过HTTP使用XML与我们的供应商通信

到目前为止,我的要求是:

POST /some-service HTTP/1.1
Host: me
Content-Type: text/xml; charset=utf-8
Content-Length: 470

<?xml version="1.0" ?>
<PNARequest>
    <Version>2.0</Version>
    <TransactionHeader>
        <SenderID>My Company</SenderID>
        <ReceiverID>My Supplier</ReceiverID>
        <CountryCode>FR</CountryCode>
        <LoginID>My Login</LoginID>
        <Password>My Password</Password>
        <TransactionID>some hexa numbers</TransactionID>
    </TransactionHeader>
    <PNAInformation Quantity="1" SKU="more numbers here"/>
    <ShowDetail>2</ShowDetail>
</PNARequest>
Python使用:print represponse显示了这一点

这是完整的反应,不仅仅是身体。没有标题,没有那样的

在供应商方面,他们甚至没有在他们的日志中看到我的请求。那么,有什么线索吗?任何帮助都将不胜感激

谢谢

更新

下面是我发送此请求的Python代码注意,这只是一个测试通信过程的脚本,看看真正交换了什么:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re
import socket

def condense(body):
    body = re.sub(' +<','<', body)
    return body.replace('\n', '')

def post(host, port, api, body, content_type="text/xml; charset=utf-8", 
         me="my.ip"):
    body = condense(body)
    lines = [
        "POST {} HTTP/1.1".format(api),
        "Host: {}".format(me),
        "Content-Type: {}".format(content_type),
        "Content-Length: {}".format(len(body)),
        "",
        "{}".format(body),
        "",
        "",
    ]
    msg = "\r\n".join(lines)

    s = socket.create_connection((host, port))
    s.send(msg)
    print "REQUEST ##############################################"
    print msg

    res = ""
    data = True
    while data:
        data = s.recv(8192)
        res += data

    s.close()
    print "RESPONSE #############################################"
    print res
    print 


def unpack_post(d, **kwargs):
    post(d["host"], d["port"], d["api"], d["body"], **kwargs)

my_supplier = {
    "host": "my.supplier.com",
    "port": 443,
    "api": "/some-api",
    "body": """<?xml version="1.0" ?>
<PNARequest>
    <Version>2.0</Version>
    <TransactionHeader>
        <SenderID>Your Company</SenderID>
        <ReceiverID>My Supplier</ReceiverID>
        <CountryCode>FR</CountryCode>
        <LoginID>My Login</LoginID>
        <Password>My Password</Password>
        <TransactionID>Some Hexa Numbers</TransactionID>
    </TransactionHeader>
    <PNAInformation Quantity="1" SKU="More Numbers Here"/>
    <ShowDetail>2</ShowDetail>
</PNARequest>""",
}

if __name__ == "__main__":
    unpack_post(my_supplier)
我知道这有点难看,但我这么做只是为了测试,所以。。。
我写这段代码是因为我喜欢在多台服务器上测试,比如,…

所以,正如我在一篇评论中所说的,这可能是由于服务器端的问题

我知道这不是一个完美的答案,但由于我的供应商没有与我分享他们的日志,我只能猜测事情


如果我发现有新的东西要分享,我会稍后编辑此答案,但我不会,因为这不再是一个编程问题,我想在这里,这不是一个合适的地方。

请详细说明如何发送您的请求。发布代码,这样我们就可以在该部分中找到bug。如果服务器没有记录您的请求,可能它没有看到它。也许防火墙会阻止它。也许,也许,也许;这里有太多的选择。我们可以试着在我们看到的东西中找出错误,但是在我们看不到的东西中很难猜出错误,所以请提供你所拥有的一切。我在这里是为了防止有人有同样的问题。因为,事实上,它取决于服务器端的很多事情。。。此外,我今天刚收到一封来自供应商的电子邮件,可以解释问题,但他们没有解释他们正在做什么。。。但是为了社区的利益,我会发布我的代码,希望有人会认为这很有帮助。我的英语不好,请原谅我⁺对于您的努力:谢谢!也许我现在应该自己回答,因为正如我所说的,这可能是服务器端的问题。你怎么想?所以你应该接受你自己的答案。这样,这个问题就不再列在未回答的公开问题中了。所以,是的,说吧!明天你可以接受你自己的答案。该死的,我一定不能忘记!
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re
import socket

def condense(body):
    body = re.sub(' +<','<', body)
    return body.replace('\n', '')

def post(host, port, api, body, content_type="text/xml; charset=utf-8", 
         me="my.ip"):
    body = condense(body)
    lines = [
        "POST {} HTTP/1.1".format(api),
        "Host: {}".format(me),
        "Content-Type: {}".format(content_type),
        "Content-Length: {}".format(len(body)),
        "",
        "{}".format(body),
        "",
        "",
    ]
    msg = "\r\n".join(lines)

    s = socket.create_connection((host, port))
    s.send(msg)
    print "REQUEST ##############################################"
    print msg

    res = ""
    data = True
    while data:
        data = s.recv(8192)
        res += data

    s.close()
    print "RESPONSE #############################################"
    print res
    print 


def unpack_post(d, **kwargs):
    post(d["host"], d["port"], d["api"], d["body"], **kwargs)

my_supplier = {
    "host": "my.supplier.com",
    "port": 443,
    "api": "/some-api",
    "body": """<?xml version="1.0" ?>
<PNARequest>
    <Version>2.0</Version>
    <TransactionHeader>
        <SenderID>Your Company</SenderID>
        <ReceiverID>My Supplier</ReceiverID>
        <CountryCode>FR</CountryCode>
        <LoginID>My Login</LoginID>
        <Password>My Password</Password>
        <TransactionID>Some Hexa Numbers</TransactionID>
    </TransactionHeader>
    <PNAInformation Quantity="1" SKU="More Numbers Here"/>
    <ShowDetail>2</ShowDetail>
</PNARequest>""",
}

if __name__ == "__main__":
    unpack_post(my_supplier)