`
xiaolin0199
  • 浏览: 573366 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

传说中只用了十五行Python语句的P2P客户端/服务端 [转]

阅读更多

#zz from http://www.exonsoft.com/~kochin/TinyP2P/tinyp2p.html
# tinyp2p.py 1.0 (documentation at http://freedom-to-tinker.com/tinyp2p.html)
# (C) 2004, E.W. Felten
# license: http://creativecommons.org/licenses/by-nc-sa/2.0


# Annotated by Kochin Chang, Jan. 2005


# Usage:
#   Server - python tinyp2p.py password server hostname  portnum [otherurl]
#   Client - python tinyp2p.py password client serverurl pattern
#                   ar[0]      ar[1]    ar[2]  ar[3]     ar[4]   ar[5]


import sys, os, SimpleXMLRPCServer, xmlrpclib, re, hmac 
# Import libraries used in the program.
# sys : system variables and functions.
# os : portable OS dependent functionalities.
# SimpleXMLRPCServer : basic XML-RPC server framework.
# xmlrpclib : XML-RPC client support.
# re : regular expression support.
# hmac : RFC 2104 Keyed-Hashing Message Authentication.


ar,pw,res = (sys.argv,lambda u:hmac.new(sys.argv[1],u).hexdigest(),re.search)
# A multiple assignment.
# ar <- sys.argv : the argument list.
# pw <- lambda u:hmac.new(sys.argv[1],u).hexdigest() :
#   a function makes an HMAC digest from a URL.
#   INPUT: a string, u, which is a URL here.
#   OUTPUT: a hexdecimal HMAC digest.
#   DESCRIPTION:
#     1. Creates a HMAC object from the URL using network's password,
#        sys.argv[1], as the key.
#     2. Returns a hexdecimal digest of the HMAC object.
# res <- re.search : alias for the regular expression search function.

pxy,xs = (xmlrpclib.ServerProxy,SimpleXMLRPCServer.SimpleXMLRPCServer)
# A multiple assignment.
# pxy <- xmlrpclib.ServerProxy : alias for the ServerProxy class.
# xs <- SimpleXMLRPCServer.SimpleXMLRPCServer : alias for the SimpleXMLRPCServer class.

def ls(p=""):return filter(lambda n:(p=="")or res(p,n),os.listdir(os.getcwd()))
# a function lists directory entries.
# INPUT: a string, p, which is a regular expression pattern.
# OUTPUT: a list of directory entries matched the pattern.
# DESCRIPTION:
#   1. Creates a function using lambda expression that takes a pathname as its
#      parameter. The function returns true if the pattern is empty or the
#      pathname matches the pattern.
#   2. Finds out what is the current working directory.
#   3. Retrieves a list of directory entries of current working directory.
#   4. Filters the list using the lambda function defined.

if ar[2]!="client":
# Running in server mode...
  
  myU,prs,srv = ("http://"+ar[3]+":"+ar[4], ar[5:],lambda x:x.serve_forever())
  # A multiple assignment.
  # myU <- "http://"+ar[3]+":"+ar[4] : server's own URL.
  # prs <- ar[5:] : URL's of other servers in the network.
  # srv <- lambda x:x.serve_forever() :
  #   a function to start a SimpleXMLRPCServer.
  #   INPUT: a SimpleXMLRPCServer object, x.
  #   OUTPUT: (none)
  #   DESCRIPTION:
  #     Calls the server's serve_forever() method to start handling request.
  
  def pr(x=[]): return ([(y in prs) or prs.append(y) for y in x] or 1) and prs
  # a function returns the server list.
  # INPUT: a list, x, of servers' URLs to be added to the server list.
  # OUTPUT: the updated server list.
  # DESCRIPTION:
  #   1. For each URL in x, checks whether it's already in the server list.
  #      If it's not in the list, appends in onto the list.
  #   2. Returns the updated server list.
  
  def c(n): return ((lambda f: (f.read(), f.close()))(file(n)))[0]
  # a function returns content of the specified file.
  # INPUT: a string, n, which is a filename.
  # OUTPUT: the content of the file in a string.
  # DESCRIPTION:
  #   1. Creates a function using lambda expression that takes a file object, f,
  #      as its parameter. The function reads the content of the file, then
  #      closes it. The results of the read and close are put into a tuple, and
  #      the tuple is returned.
  #   2. Creates a file object with the filename. Passes it to the lambda
  #      function.
  #   3. Retrieves and returns the first item returned from the lambda function.
  
  f=lambda p,n,a:(p==pw(myU))and(((n==0)and pr(a))or((n==1)and [ls(a)])or c(a))
  #   a request handling function, depending on the mode, returns server list,
  #   directory entries, or content of a file.
  #   INPUT: a string, p, which is a hexdecimal HMAC digest.
  #          a mode number, n.
  #          if n is 0, a is a list of servers to be added to server list.
  #          if n is 1, a is a pattern string.
  #          if n is anything else, a is a filename.
  #   OUTPUT: if n is 0, returns the server list.
  #           if n is 1, returns directory entries match the pattern.
  #           if n is anything else, returns content of the file.
  #   DESCRIPTION:
  #     1. Verifies the password by comparing the HMAC digest received and the
  #        one created itself. Continues only when they match.
  #     2. If n is 0, calls pr() to add list, a, and returns the result.
  #        If n is 1, calls ls() to list entries match pattern a, and returns
  #        the result enclosed in a list.
  #        If n is any other value, retreives and return content of the file
  #        with filename specified in a.
  
  def aug(u): return ((u==myU) and pr()) or pr(pxy(u).f(pw(u),0,pr([myU])))
  # a function augments the network.
  # INPUT: a string, u, which is a URL.
  # OUTPUT: a list of URL's of servers in the network.
  # DESCRIPTION:
  #   1. If the URL, u, equals to my own URL, just returns the server list.
  #   2. Otherwise, creates a ServerProxy object for server u. Then calls its
  #      request handling function f with a HMAC digest, mode 0, and server
  #      list with myself added.
  #   3. Calls pr() with the result returned from server u to add them to my
  #      own list.
  #   4. Returns the new list.
  
  pr() and [aug(s) for s in aug(pr()[0])]
  # 1. Checks the server list is not empty.
  # 2. Takes the first server on the list. Asks that server to augment its
  #    server list with my URL.
  # 3. For each server on the returned list, asks it to add this server to its
  #    list.
  
  (lambda sv:sv.register_function(f,"f") or srv(sv))(xs((ar[3],int(ar[4]))))
  # Starts request processing.
  # 1. Defines a function with lambda expression that takes a SimpleXMLRPCServer
  #    object, registers request handling function, f, and starts the server.
  # 2. Creates a SimpleXMLRPCServer object using hostname (ar[3]) and portnum
  #    (ar[4]). Then feeds the object to the lambda function.
  
# Running in client mode...
for url in pxy(ar[3]).f(pw(ar[3]),0,[]):
# 1. Create a ServerProxy object using the serverurl (ar[3]).
# 2. Calls the remote server and retrieves a server list.
# 3. For each URL on the list, do the following:

  for fn in filter(lambda n:not n in ls(), (pxy(url).f(pw(url),1,ar[4]))[0]):
  # 1. Create a ServerProxy object using the URL.
  # 2. Calls the remote server to return a list of filenames matching the
  #    pattern (ar[4]).
  # 3. For each filename doesn't exist locally, do the following:
  
    (lambda fi:fi.write(pxy(url).f(pw(url),2,fn)) or fi.close())(file(fn,"wc"))
    # 1. Define a lambda function that takes a file object, calls remote server
    #    for the file content, then closes the file.
    # 2. Create a file object in write and binary mode with the filename. (I
    #    think the mode "wc" should be "wb".)
    # 3. Passes the file object to the lambda function.
分享到:
评论
1 楼 fire01312 2010-05-27  
这么强啊!

相关推荐

    基于C语言核心的跨平台HTTP客户端/服务端库soup设计源码

    本项目是一款基于C语言核心的跨平台HTTP客户端/服务端软件库——soup的设计源码,包含372个文件...该库支持C、C、HTML、JavaScript、PHP、Shell、CSS和Python等多种语言,旨在提供高效、灵活的HTTP客户端和服务端功能。

    流式套接字实现简单的客户端服务端通信过程

    - 基于TCP的简单聊天程序:服务端接收多个客户端连接,每个客户端可以发送消息给服务端,服务端广播消息给所有在线客户端。 - 文件传输应用:客户端请求服务端的文件,服务端读取文件并分块发送,客户端接收并拼接...

    Python3 实现的HTTP服务端(server)和客户端(clients)通信

    在Python3中,进行HTTP服务端与客户端的通信是网络编程的一个重要方面。HTTP(超文本传输协议)是互联网上应用最广泛的一种网络协议,用于从万维网服务器传输超文本到本地浏览器的传输协议。Python3提供了丰富的库来...

    Python 聊天室 程序 客户端 服务端

    本项目包含客户端(Client.py)和服务端(Server.py),适用于初学者掌握基础的网络编程技能。我们将深入探讨这两个核心文件以及相关知识点。 首先,我们要了解Python中的网络编程。在Python中,socket模块提供了低...

    Python 网络编程之TCP客户端/服务端功能示例【基于socket套接字】

    本文实例讲述了Python 网络编程之TCP客户端/服务端功能。分享给大家供大家参考,具体如下: demo.py(TCP客户端): import socket def main(): # 1. 创建tcp的套接字 tcp_socket = socket.socket(socket.AF_INET...

    基于PyQt5的TCP文件传输程序python源码(含客户端+服务端).zip

    基于PyQt5的TCP文件传输程序python源码(含客户端+服务端).zip基于PyQt5的TCP文件传输程序python源码(含客户端+服务端).zip基于PyQt5的TCP文件传输程序python源码(含客户端+服务端).zip基于PyQt5的TCP文件传输程序...

    基于UDP协议开发的即时聊天室python源码(含客户端+服务端).zip

    基于UDP协议开发的即时聊天室python源码(含客户端+服务端).zip基于UDP协议开发的即时聊天室python源码(含客户端+服务端).zip基于UDP协议开发的即时聊天室python源码(含客户端+服务端).zip基于UDP协议开发的即时聊天...

    Python TCP通信 客户端 服务端

    本篇将深入探讨如何使用Python实现TCP(传输控制协议)通信,包括客户端和服务端的构建,并结合图形用户界面(GUI)提升用户体验。 TCP是一种面向连接的、可靠的、基于字节流的传输层通信协议,确保数据的顺序传输...

    ice客户端和服务端

    标题中的“ice客户端和服务端”指的是ZeroC的ICE(Internet Communications Engine)框架,这是一个高性能、跨平台的中间件,用于构建分布式系统。ICE提供了一种面向对象的接口,支持多种编程语言,包括C++、Java、...

    客户端和服务端源代码

    客户端和服务端源代码是软件开发中的核心组成部分,它们构成了应用程序的两大部分,使得用户能够通过网络进行交互。在这个特定的压缩包中,虽然文件名为"TEST",但我们可以通过理解客户端和服务端的基本概念来深入...

    Python 模拟http服务端, 客户端

    Python 模拟http服务端, 客户端

    python网络编程socket实现服务端、客户端操作详解

    在Python中使用Socket实现TCP服务端和客户端的基本步骤如下: ### TCP服务端操作 1. **创建Socket**: 使用`socket.socket()`函数创建Socket对象,参数默认为`socket.AF_INET`(IPv4)和`socket.SOCK_STREAM`...

    基于python+pyqt5开发的拍卖行程序源码(含客户端和服务端)+项目说明.zip

    基于python+pyqt5开发的拍卖行程序源码(含客户端和服务端)+项目说明.zip基于python+pyqt5开发的拍卖行程序源码(含客户端和服务端)+项目说明.zip基于python+pyqt5开发的拍卖行程序源码(含客户端和服务端)+项目说明....

    TCP客户端和服务端代码

    在压缩包中的"TCP客户端和服务端代码"可能包含了具体的实现细节,包括但不限于上述步骤。通过学习和分析这些代码,我们可以更好地理解TCP通信的工作原理,并将这些知识应用于实际项目中。无论是开发服务器端应用还是...

    redis客户端和服务端

    在 Redis 的架构中,客户端(Client)和服务端(Server)是两个关键组件,它们之间的交互构成了 Redis 数据操作的基础。 **Redis 客户端** Redis 客户端是应用程序与 Redis 服务器进行通信的接口。它负责建立连接...

    斗地主服务端源码(含客户端程序)lua c# python

    斗地主服务端源码是开发在线斗地主游戏的核心部分,它负责处理游戏逻辑、玩家交互、数据存储以及网络通信等关键任务。本源码包含客户端程序,提供了一个完整的系统,供开发者学习和研究。涉及到的主要编程语言有Lua...

    基于python+opencv人脸识别的签到管理系统源码带界面+演示视频+设计报告(含客户端+服务端).zip

    基于python+opencv人脸识别的签到管理系统源码带界面+演示视频+设计报告(含客户端+服务端).zip基于python+opencv人脸识别的签到管理系统源码带界面+演示视频+设计报告(含客户端+服务端).zip基于python+opencv...

    python socket实现客户端服务端通信列子

    标题中的"python socket实现客户端服务端通信列子"指的就是使用Python的socket库来创建一个客户端(client)和一个服务器端(server),以便它们可以通过网络进行交互。这个过程通常包括以下几个步骤: 1. **创建...

    android客户端+服务端

    在Android应用开发中,"客户端+服务端"的架构模式是常见的设计方式,它涉及到Android客户端与远程服务器之间的数据通信和交互。这种模式允许应用程序在本地执行用户界面操作,同时利用服务器的计算能力和存储资源来...

Global site tag (gtag.js) - Google Analytics