简体   繁体   中英

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. Suppose the server I want it to send is https://some/server and my proxy is proxy:port_number . I tried to use httplib2 with SocksiPy in the following way:

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). I don't have any certificate information - similar working Perl script doesn't use one, it defines proxies as Environment variables. 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). Please point me on how to do this.

Maybe it will be appropriate to use httplib with xmlrpclib ? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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