XMLRPC(客户端/代理)在防火墙后面使用 - Python 2.7

时间:2013-11-28 15:57:18

标签: python-2.7 proxy urllib2 xml-rpc xmlrpclib

我是XMLRPC的新手,但我需要使用它(Python 2.7中的xmlrpclib)与接受xml文件的服务器(www.neos-server.org)进行通信。我是一个严重限制传出和传入流量的防火墙,但我能够使用webproxy大部分畅通无阻地浏览网页。

测试1和2(下面)工作,但测试3导致HTTP 502错误(cantconnect)

import urllib2
import xmlrpclib
import httplib

class ProxyTransport(xmlrpclib.Transport):
    def request(self, host, handler, request_body, verbose):

        self.verbose = verbose
        url = 'http://' + host + handler
        if self.verbose: "ProxyTransport URL: [%s]" % url

        request = urllib2.Request(url)
        request.add_data(request_body)
        request.add_header("User-Agent", self.user_agent)
        request.add_header("Content-Type", "text/xml")

        proxy_handler = urllib2.ProxyHandler({"http":"MYPROXY:8080"})
        opener = urllib2.build_opener(proxy_handler)

        f = opener.open(request)

        return(self.parse_response(f))

# TEST 1 - HTML fetching
def test1():
    html = urllib2.urlopen("http://www.google.com").read() # note no proxy setup here
    print html

# TEST 2 - XMLRPC sample server fetching
def test2():
    p = ProxyTransport()
    test_url = "http://betty.userland.com"
    #test_server = xmlrpclib.Server(test_url) # gives <ProtocolError for betty.userland.com/RPC2: 403 WebTrap>
    test_server = xmlrpclib.Server(test_url, transport=p) 

    test_api = "examples.getStateName(9)"
    print "API: %s" % test_api
    r = eval("test_server.%s" % test_api)
    print "Result: ", r

# TEST 3 - XMLRPC server (NEOS)
def test3():
    # Setup proxy and server
    p = ProxyTransport()
    NEOS_HOST = "www.neos-server.org"
    NEOS_PORT = 3332
    neos = xmlrpclib.Server("http://%s:%d" % (NEOS_HOST, NEOS_PORT), transport = p)

    # Talk
    print "Ping Neos..."    
    neos.ping()

test1()
test2()
test3()

我尝试了几种不同的解决方案(https://gist.github.com/nathforge/980961https://mail.python.org/pipermail/python-list/2006-July/367049.html),但它们似乎没有用。我假设我需要能够在发送/接收xml文件和结果之前ping。我接下来应该尝试什么?

0 个答案:

没有答案