简体   繁体   English

通过Python中的代理将XML数据发布到服务器

[英]POST XML data to server via proxy in Python

I need to send a request that contains the data in the following format: 我需要发送包含以下格式数据的请求:

parameterss = urlencode({'various': 'credentials', ... ,
'xmlquery': '<?xml version="1.0" encoding="UTF-8"?> \
         <query>...</query>'})

Generally, it contains an XML query. 通常,它包含一个XML查询。 Suppose the server I want it to send is https://some/server and my proxy is proxy:port_number . 假设我要发送的服务器是https://some/server ,我的代理服务器是proxy:port_number I tried to use httplib2 with SocksiPy in the following way: 我试图通过以下方式将httplib2SocksiPy结合使用:

import httplib2, socks
from urllib import urlencode
proxy = httplib2.ProxyInfo(proxy_type=socks.PROXY_TYPE_HTTP, proxy_host='proxy', proxy_port=port_number)
http = httplib2.Http(proxy_info=proxy)
response, content = http.request("https://some/server", "POST", parameters)
print response

and it works but the response is not the desired one. 它有效,但响应不是所需的。 Obviously because I need to use HTTPS proxy (which is the same). 显然是因为我需要使用HTTPS代理(相同)。 I don't have any certificate information - similar working Perl script doesn't use one, it defines proxies as Environment variables. 我没有任何证书信息-类似的工作Perl脚本不使用证书信息,它将代理定义为环境变量。 SocksiPy has PROXY_TYPE_SOCKS4 and PROXY_TYPE_SOCKS5 constants however, I don't know how to use those (without defining certificate it throws exceptions). SocksiPy具有PROXY_TYPE_SOCKS4PROXY_TYPE_SOCKS5常量,但是,我不知道如何使用它们(没有定义证书会抛出异常)。 Please point me on how to do this. 请指出如何执行此操作。

Maybe it will be appropriate to use httplib with xmlrpclib ? 也许将httplibxmlrpclib一起使用是合适的吗? How to define those and put together to work? 如何定义它们并组合在一起工作?

Finally I figured out the way and would like to share. 最后,我想出了办法,并希望分享。 It was much simpler than expected: 它比预期的要简单得多:

    import urllib, urllib2
    parameters = urllib.urlencode({'various': 'credentials', ... ,
    'xmlquery': '<?xml version="1.0" encoding="UTF-8"?> \
     <query>...</query>'})
    request = urllib2.Request("https://some/server", parameters)
    response = urllib2.urlopen(request)
    print response

I don't know why but it worked without defining any proxy in the code - I just defined it on the server via environment variables in the shell. 我不知道为什么,但是它没有在代码中定义任何代理就可以工作-我只是通过外壳中的环境变量在服务器上定义了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM