 Hello everyone, welcome back to some more Python programming videos. We're still looking at XML RPC lib or now We're kind of actually going to move on from XML RPC lib anyway We're going to move we're going to move into creating a server with XML RPC rather than the client access We've been using with the XML RPC lib module So if you kind of browse through and peruse through more of the Python documentation for the XML RPC lib module You'll notice way down at the bottom. It shows off how to use the Simple XML RPC server or at least it shows some examples of using those So I was a little curious and wanted to learn a little bit more about them So let's let's actually do a video on that, you know, let's let's show off how to how to use these So I want to know the documentation for this guy, so I'm gonna Google that Python Simple XML RPC server So this guy at this module is a library that provides basic server Framework for XML RPC servers written in Python Servers can either be freestanding using simple XML RPC server or embedded in a CGI environment using CGI XML RPC request handler I'm not going to get into the CGI version if you want to do that research on your own Of course you can and notice again at the top here There is a note that this module has been merged to XML RPC server and Python 3 We had the same thing with XML RPC clients with XML RPC lib So just so you know if you're using Python 3 you got your 2-3 tool to do whatever you need to do But blah blah blah Just just have that note and thing in your mind So this module has one class or well, I guess multiple classes But the one that I want to show off and use is a simple XML RPC server class now this takes an address and From what I've seen this address is often a tuple with the address and port that we're gonna be using it on So for our example, yeah, we'll use localhost and 8000 and we'll just kind of go through with some of these Examples, I'll just demonstrate them and show you them for you. So let's kind of go through with it We created the new server instance and Let's let's just do that. I'll fire up idle and I'm gonna import simple XML RPC server That's right, isn't it? Yep. Alright import cool now I'm just gonna copy this and And I'll say server equal this and the address we'll use Thanks, I'll complete didn't really need that there localhost Hoises to and 8000 to be your port. That's just fine Cool. So now we've got our server server object and What we'll do now that we've got that created Let's go through a little bit more of these uh this documentation once we've created the object You can see that the server class is based on socket server tcp server And provides the means of creating a simple standalone xml RPC server It might be some it might be worthwhile to kind of see what this contains Sometime do some research on the socket server tcp server. So What we can do is we can register functions Register a function that can respond to XML RPC requests If name is given it will be given the method name associated with function Otherwise function dot name will be used the actual name of the function itself And the name argument can be either a normal or unicode string Make contain characters not legal in python identifiers including the period character huh Now that's because we were able to see things like sample dot add in the last tutorial in the last video so Um Allow dotted names is for register instance, which I probably won't go through register introspection Functions should be called But we'll play with it and see if that works for us multi call functions. We'll get into And it goes thresholds RPC paths So stuff to read about and stuff to learn if you want to go through with it But let's check out what we've got here Let's create a new function. Let's call ours add just like the example. They were using x and y And we'll return x plus y Simple stuff server dot register function See the caps lock And the function will be add And i'm not going to supply a name variable there So it'll just use our name variable That comes with the function like the actual name of the function add So i'm going to create a new idle session And what i'll do is connect to the server But we have to actually turn the server on right? I know I saw a function earlier that was like serve forever Yeah server dot serve forever Is that in the documentation serve forever? Huh it shows it in examples, but doesn't actually show any uh Documentation about it, but i'm sure that's the way that's actually running the main loop We could saw that we actually saw that in the uh In the examples here run the server's main loop would serve forever. Let's try server dot serve forever Okay, and now it's doing its thing So now in my other python shell in my other idle session Let's try importing xml rpc lib I wonder if I can connect to it xml rpc lib I'll say client can equal xml rpc dot lib dot server proxy And ur i can be localhost Unsupported xml rpc protocol. I think we might need to specify the 8 000 port here. Let's try it No, okay Uh, I would think we need to supply http over here Oh Okay, that works just fine for so I use http colon to forward slashes to represent hypertext transfer protocol Localhost being the name of it and I supplied 8 000 as my port the colon there So there we go that works for us now if I try and use client dot system It doesn't get anything If I try and use client system dot list methods It's going to throw an error at me. It says system list methods is not supported Well, why is that? We can see in our server. Okay, people are trying to actually call from it. We're trying to get some Methods and information from it. That's right here at our localhost address So i'm going to hit control c and interrupt the server So now what we can do is we can go ahead and call that uh function that we saw up top here The register introspection functions So this will let us actually test for the functions that we have seen Have I still got my server going good server dot register introspection functions So now we'll be able to use everything in that system subclass If I turn the server on server forever Let's go ahead and connect to it client create that new session Now if I use client system dot list methods Hey, we got everything that we saw earlier and we've got our add function which we registered here Now I can run this client dot add Now note i'm not using sample or anything preceding it because in our list methods It returns all the names of functions. So this truly is the name here if I add three and seven We'll be returned to us 10 and you can see in our server. Hey, it's trying it's reading these It's reading these requests from us HTTP post requests cool So let's add a new function to our server. I'm gonna I'm gonna close it And I'll do server Actually, we had to define a new function. Let's say define Subtraction function This will be simple take x and y return x y turn x minus y server dot register function Registers a function to respond to the x and y A rpc request will pass in our subtraction function And this time I will supply a name for the name uh argument and we'll just say sup or Subtract So now that's registered. We'll call our server to serve forever We'll connect to it client equals this client dot system List methods And now we've got our subtract function client dot subtract Four and one we should get three three sweet. That's fantastic I am a little curious and it might take a little bit more research on my end as to where we can get the method help Information where can we set that? client dot system dot method help Now it needs a string. So let's say add And nothing is returned I wonder if it's got to be like in the in like documentation strings for the functions If I subtract if I define sorry my subtraction function with documentation strings You know three quotes Because here if I were to say subtract two numbers from each other subtraction x y will return x minus y now that that's saved if I Add this to our server Turn the server on now we can recall the client if I call client system Method help. Okay. We are connected. Cool method help if I want to get information on the subtract function Hey, cool. We get that documentation string subtract two numbers from each other new line subtraction x y returns x minus y So that's how you can kind of give your function a description in your server by having these the documentation Strings inside the function and kind of determining what everything will do You can write what arguments it takes what it will return what the function does that sort of thing It's all up to you And that's kind of the beauty of it So there it is. There's kind of your toolkit for a simple xml rpc server You can add as many functions as you would like Um kind of save whatever you need to do it with and xml rpc can be written in just about any language But we're using it in python right now. So just as a really cool thing You can pass data to something online have it exist online give it back to you And that's really the beauty of this xml rpc server and of course the client that we've learned how to use So thanks for watching everybody. I hope you enjoyed this Uh, I hope you do kind of understand now how to set up your own server with this in python And the cool things you can do the functions that it takes to run it and that sort of thing Uh, all right, I'll I'll get out of your hair now, but thank you again for watching I hope to see you in the next tutorial. Bye