Reputation: 19
I need to create two Python programs, Server.py and Client.py. Server.py should register x procedures that the client will be able to call. It will then bind to the address "localhost" and port 8000.
Server.py should support the following procedures:
"name" returns the name of the server which is passed on the command line during server invocation
"help" returns a list of procedures that the server supports
"servertime" returns the current time at the server in 24 hour format
add(x,y) returns the sum of x and y
sub(x,y) returns x - y
mult(x, y) returns x * y
div(x, y) returns x / y, including handling a divide by zero scenario
For Client.py, the client connects to the server using the server's address and the port the server is listening on (in this case, localhost and 8000). x and y will equal 8 and 6, and will be then pass on to those different functions listed in Server.py
This is the code I have for both programs:
Server.py
import sys
import time
from xmlrpc.server import SimpleXMLRPCServer
host_address = sys.argv[1]
host_port = sys.argv[2]
server = SimpleXMLRPCServer((str(hostAddress), int(port)))
def add(x, y):
return x + y
def sub(x, y):
return x - y
def mult(x, y):
return x * y
def div(x, y):
try:
return x / y
except ZeroDivisionError:
print(x + " divided by " + y + " is infinity.")
def name():
return hostAddress
def helpFunc():
return server.system_listMethods()
def servertime():
return time.strftime("%H:%M:%S")
server.register_function(name)
server.register_function(helpFunc)
server.register_function(add)
server.register_function(servertime)
server.register_function(sub)
server.register_function(mult)
server.register_function(div)
server.serve_forever()
Client.py
import xmlrpc.client
import sys
from xmlrpc.server import SimpleXMLRPCServer
host_address = sys.argv[1]
host_port = sys.argv[2]
x = sys.argv[3]
y = sys.argv[4]
URI = "http://" + host_address + ":" + host_port
proxy = xmlrpc.client.ServerProxy(URI)
print('{} + {} is {}'.format(x, y, str(proxy.add(x, y))))
print('{} - {} is {}'.format(x, y, str(proxy.sub(x, y))))
print('{} * {} is {}'.format(x, y, str(proxy.mult(x, y))))
print('{} / {} is {}'.format(x, y, str(proxy.div(x, y))))
print(proxy.name())
print(proxy.helpFunc())
print(proxy.servertime())
When I run the programs, however, I get the following error:
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
Could I please get some help on this?
Upvotes: 0
Views: 36