Python: Is it possible to set the clientport with xmlrpclib?

三世轮回 提交于 2019-12-12 02:06:44

问题


Is it possible to set the clientport for the xmlrpc-connection?

I want to say:

Client should make a ServerProxy-object to over a specific client port

or pseudocode something like this:

serv = xmlrpclib.ServerProxy("server:port","overSpecificClientPort").

回答1:


Try to define a custom transport. This should be something like that:

import xmlrpclib, httplib

class sourcedTransport(xmlrpclib.Transport):
    def setSource(self, src):
        self.src = src
    def make_connection(self, host):
        h = httplib.HTTPConnection(host, source_address= self.src)
        return h

srcPort = 43040
srcAddress = ('', srcPort)
p = sourcedTransport()
p.setSource(srcAddress)
server = xmlrpclib.ServerProxy("server:port", transport=p)

EDIT: bug fix httplib.HTTP => httplib.HTTPConnection

And checked that it works, in python 2.7 (but not before)




回答2:


There is no option for this in module xmlrpclib, but you can create your own by modifying the original version. Assuming you use Linux, fetch /usr/lib/python2.7/xmlrpclib.py. Modify the method make_connection accordingly.

Providing a parameter source_address to HTTPConnection is supported by httplib not before Python version 2.7.

Have fun!



来源:https://stackoverflow.com/questions/7269849/python-is-it-possible-to-set-the-clientport-with-xmlrpclib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!