Python practice 3-XML-RPC To achieve simple Peer-to-peer file sharing __python

Source: Internet
Author: User
XML-RPC realizes simple Peer-to-peer file sharing

First to Baidu Encyclopedia:

The full name of XML-RPC is the remote procedure call that is Procedure called, XML (a subset of the standard Universal Markup Language). It is a set of specifications and a series of implementations that allow programs in different operating systems and environments to implement internet-based process calls. This remote procedure call uses HTTP as the transport protocol, and XML is used as the encoding format for transmitting information. The definition of XML-RPC is as simple as possible, but simultaneously capable of transmitting, processing, and returning complex data structures. This process is also known as "distributed Computing".

Study Data address:

Https://docs.python.org/3/library/xmlrpc.html

Https://www.the5fire.com/python-project8-xml-rpc.html

Here is the test example: Environment Python 3.6 [version less than 3.x need to change part of the code]

Service side

From Xmlrpc.server import simplexmlrpcserver
s = Simplexmlrpcserver (("", 4242))
def twice (x): Return
    x*2
s.register_function (twice) 

Client

Import xmlrpc.client
s = xmlrpc.client.ServerProxy (' http://localhost:4242 ')
print (S.twice (2))

Execution results

Then, to the point, write a file sharing example (Python 3.6):

The example is very simple, look at the notes for details.

server.py

From Xmlrpc.server import simplexmlrpcserver to xmlrpc.client import serverproxy,fault from Os.path import Join,abspath , isfile import sys import urllib simplexmlrpcserver.allow_reuse_address = 1 Max_history_length = 6 unhandled = 1
        Access_enied = Class Unhandledquery (Fault): Def __init__ (self, message= "couldn ' t handle" query "):
        Fault.__init__ (self, unhandled, message) class AccessDenied (Fault): def __init__ (self,message= "Access denied"):
    Fault.__init__ (self, access_enied, message) def inside (dir, name): dir = Abspath (dir) name = Abspath (name) Return Name.startswith (Join (dir, ")" Def getport (URL): name = Urllib.parse.urlparse (URL) [1] patrs = NAME.SPL
        It (': ') return int (patrs[-1]) class Node:def __init__ (self, url, dirname, secret): Self.url = URL Self.dirname = dirname Self.secret = Secret Self.known = set () def query (self, query, history=[])
          : Try:  return Self._handle (query) except unhandledquery:history = history + [Self.url] If Len (
        History) >= max_history_length:raise return self._broadcast (query, history) def hello (self, Other): Self.known.add (Other) return 0 def fetch (self, Query, secret): If Secret!= self.secret:raise Ac
        cessdenied result = self.query (query) F = open (Join (self.dirname,query), ' W ") f.write (Result) F.close () return 0 def _start (self): s = Simplexmlrpcserver (("", Getport (Self.url)), LOGREQUESTS=FA
        LSE) S.register_instance (self) s.serve_forever () def _handle (self, query): dir = self.dirname  Name = Join (dir, query) if not Isfile (name): Raise Unhandledquery if not inside (dir, name): Raise accessdenied return open (name). Read () def _broadcast (self, Query, history): For the other in SELF.KNOWN.C Opy (): If OTHer in history:continue try:s = Serverproxy (Other) return s.query (query, HI Story) except Fault:self.known.remove (other) #if Fault.faultcode = = Unhandled
        : Pass #else: Self.known.remove (Other) Except:self.known.remove (other) Raise Unhandledquery def Main (): url,directory, Secret = sys.argv[1:] n = Node (URL, directory, secret) N._st Art () if __name__ = = ' __main__ ': Main ()


client.py

From xmlrpc.client import serverproxy,fault from cmd import cmd to random import choice from string import Ascii_lowerca SE from server import node,unhandled from threading import Thread to time import sleep import sys head_start = 0.1 # S  Econds secret_lenfgth = def randomstring (length): chars = [] letters = ascii_lowercase[:26] while length > 0:length-= 1 chars.append (choice (Letters)) return '. Join (Chars) class Client (CMD): Prom PT = ' > ' def __init__ (self, URL, dirname, urlfile): cmd.__init__ (self) Self.secret = RandomString (
        secret_lenfgth) n = Node (URL, dirname, self.secret) t = Thread (Target=n._start) T.setdaemon (1)
            T.start () sleep (head_start) self.server = serverproxy (URL) for line in open (Urlfile): line = Line.strip () Self.server.hello (line) def do_fetch (self, arg): try:self.se Rver.fetch (ARG, self.secret) except Fault: #if f.faultcode!= unhandled:raise print ("couldn ' t find the file" +arg def do_exit (self, arg): Print sys.exit () do_eof = Do_exit def main (): Urlfile,directory,ur L = sys.argv[1:] client = client (URL, directory, urlfile) Client.cmdloop () if __name__ = = ' __main__ ': Main ()

Repeated the above code workflow, mainly divided into two parts, service and Client,service is mainly to maintain some node information, the client is mainly used for the current user to send requests and interaction places.

The portal begins with the client, and the main function comes in to accept several parameters:

(1) Urlfile.txt path, this is a file, which contains a number of other nodes of the link URL and port, simulation of the current node user's "partner", if all nodes as a map, a->b a->c a->d Then the utlfile in the A node is the link information of the BCD. Of course this is to simulate, the actual situation may be in a location to get, or in the process of accumulation.

(2) The second parameter directory path, which is to point to a path, as a shared folder for the current node.

(3) The last URL parameter is the URL and port that the node of the Local service establishes the service, for example, to open the Node node service of the local port 2333, to pass the http://localhost:2333 directly.

When you get the parameters, you enter the client class first, randomly generating a local password in the constructor, and then creating a node node that creates a thread of the node's _start as a thread function (_start is creating the server node and opening the listener). Then in creating a serverproxy link on your own machine on this node nodes service, invoke the service's Hello function to urlfile the partner information in the inside (Hello is the stored link information table). The initialization of this client is complete.

After the client initialization, the Cmdloop, related to the Korean-style client inside the two functions Do_fetch and exit. Fetch function to get the file. The client sends the command to get the file by calling this. Here Amin is directly requesting the local node node of the FETCH function, local nodes This function after the command is first to determine the password (in fact, think this is the password is the device), and then began to enter query function to query files, query function inside is to try a wave to see the file is not local, If not, judge if the request limit is reached. If not, take out the chain of their friends, direct broadcast Call friends query function (RPC), the friend of query is this logic, local have directly returned results, did not continue to call friends friend. This place, if abstracted into a graph, is a depth-first search traversal (DFS), and of course you can find a way to BFS. The final survey results will be traced back to the start of the issue of the node, that is, the start of the client call to the FETCH function, if the file is found to save, did not find all the output related prompts on the line. This is the XMP-PRC (above code) Simple example of the implementation of ideas. Finally, the test journey.

The test steps are as follows

First you need to set up two folders, a and C,c folder to create a file, B.txt, in a and C folder to establish UrlsA.txt and urlsC.txt files. Inside, write in UrlsA.txt: http://localhost:4243, then open two command lines


Open a CMD window a

Python client.py urlsA.txt A http://localhost:4242

Fetch B.txt

After executing these two commands, you should see that the file cannot be found

Open a second window B (the first window is not closed)

Python client.py URLs C.txt C http://localhost:4243

Fetch B.txt

After executing two commands, there is no output, and the documentation is found in this place.

Looking back at this time again in the first open window to find B fetch B.txt found still not found, reasonable at this time should be able to, because the first inside has a second mailing address.

It's OK to turn the top two node back on, because when a is started, link B, but B is not, and a thinks B is off the line, and it moves b out of the list.

In turn, you will find that a can be successfully downloaded to folder A.

All the above code is a test example, there are many details are not processed, can be changed according to the actual situation. Familiarize yourself with the fundamentals of XML-RPC and Peer-to-peer.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.