0

I wrote this server using xmlrpc in python I want to be albe to access this server from any computer but it throws error. And another thing is that how can I make sure that the my server can support more than 1 client at a time?

from xmlrpc.server import SimpleXMLRPCServer
from xmlrpc.server import SimpleXMLRPCRequestHandler

# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)

# Create server
with SimpleXMLRPCServer(('82.155.18.86', 8000),
                        requestHandler=RequestHandler) as server:
    server.register_introspection_functions()

    # Register pow() function; this will use the value of
    # pow.__name__ as the name, which is just 'pow'.
    server.register_function(pow)

    # Register a function under a different name
    def adder_function(x, y):
        return x + y
    server.register_function(adder_function, 'add')

    # Register an instance; all the methods of the instance are
    # published as XML-RPC methods (in this case, just 'mul').
    class MyFuncs:
        def mul(self, x, y):
            return x * y

    server.register_instance(MyFuncs())

    # Run the server's main loop
    server.serve_forever()

and this is the client side which I want to run on another computer

import xmlrpc.client

s = xmlrpc.client.ServerProxy('http://82.155.18.86:8000')
print(s.pow(2,3))  # Returns 2**3 = 8
print(s.add(2,3))  # Returns 5
print(s.mul(5,2))  # Returns 5*2 = 10

# Print list of available methods
print(s.system.listMethods())

but this is what I get in response

C:\Users\Nima\PycharmProjects\RPC\venv\Scripts\python.exe C:/Users/Nima/PycharmProjects/RPC/main.py 
Traceback (most recent call last):
  File "C:\Users\Nima\PycharmProjects\RPC\main.py", line 4, in <module>
    print(s.pow(2,3))  # Returns 2**3 = 8
  File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\xmlrpc\client.py", line 1116, in __call__
    return self.__send(self.__name, args)
  File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\xmlrpc\client.py", line 1456, in __request
    response = self.__transport.request(
  File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\xmlrpc\client.py", line 1160, in request
    return self.single_request(host, handler, request_body, verbose)
  File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\xmlrpc\client.py", line 1172, in single_request
    http_conn = self.send_request(host, handler, request_body, verbose)
  File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\xmlrpc\client.py", line 1285, in send_request
    self.send_content(connection, request_body)
  File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\xmlrpc\client.py", line 1315, in send_content
    connection.endheaders(request_body)
  File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1250, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1010, in _send_output
    self.send(msg)
  File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 950, in send
    self.connect()
  File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 921, in connect
    self.sock = self._create_connection(
  File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\socket.py", line 843, in create_connection
    raise err
  File "C:\Users\Nima\AppData\Local\Programs\Python\Python39\lib\socket.py", line 831, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Process finished with exit code 1
Nima
  • 41
  • 1
  • 2
  • 7
  • you could also edit your post to the version working for you. That would help others experiencing similar issues. – woodz Nov 08 '22 at 22:41
  • I copy and paste the code in the doc you gave me but still no luck – Nima Nov 08 '22 at 22:58
  • please edit your post to the latest changes you have. No matter if it is sample code, just provide it below your original. Paste error logs too. Only by that way we have a base for discussion – woodz Nov 08 '22 at 23:08
  • I did, this is the exact code with just my server ID – Nima Nov 08 '22 at 23:11
  • 1
    _I want to be albe to access this server from any computer but it throws error_. A minimum knowledge of Computer networks is assumed when working with server / client stuff. What does any computer mean? Where are all your computers located? As a first step, I would suggest to work on a private LAN. So you need to use private IPs. There are lots of information on private LANs. Just read it – woodz Nov 09 '22 at 08:25
  • 1
    After you have managed it on your private LAN, move out in the public can be quite challenging and it is another story. [Here](https://stackoverflow.com/questions/62631176/python-socket-to-connect-over-global-public-ip-address) is a resource you can refer to its obstacles. It's an issue of sockets and ports, rather than one of XMLRPC. You might get some luck, if you are using `http` default ports. But even then you need some kind of port forwarding for incoming requests on your private router(s). This is a bit more advanced computer network knowledge, recommended – woodz Nov 09 '22 at 09:32
  • 1
    [socket connection over internet in python](https://stackoverflow.com/questions/35384437/socket-connection-over-internet-in-python) – woodz Nov 09 '22 at 09:36

1 Answers1

1

Have a look onto the doc.

It seems you are confusing the ports. Server port is 8000, but client tries to connect on port 3000. That won't match.

Also it is good practice to make use of with statements, like shown in the doc.

woodz
  • 737
  • 6
  • 13
  • oh my bad, I corrected that but I have another error – Nima Nov 08 '22 at 22:28
  • TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond – Nima Nov 08 '22 at 22:28
  • my advice is to follow the doc I gave you. If you have that sample in there run, you can continue with your own stuff. If that helped, I would appreciate accepting and up voting. – woodz Nov 08 '22 at 22:30