Beispiel #1
0
    def __init__(self, url, dirname, urlfile):
        """
设定 url, dirname, and urlfile ,并在单独的线程中启动Node服务器
In [8]: for line in open('./ufile'):
   ...:     line = line.strip()
   ...:     print line
www.a.com
www.b.com

        """

        Cmd.__init__(self)
        self.secret = randomString(SECRET_LENGTH)
        n = Node(url, dirname, self.secret)
        t = Thread(target=n._start)  # 这个有空再看一下相关教程和视频才行
        t.setDaemon(1)  #将该线程标记为守护进程 主线程完成就全退出
        t.start()
        # 让服务器先启动 Give
        sleep(HEAD_START)
        self.server = ServerProxy(url)
        for line in open(urlfile):
            line = line.strip()
            self.server.hello(
                line
            )  # 直接加到 known set 里面 on server.py   2019年 01月 29日 星期二 23:12:11
 def node_setup(self, url, dirname, urlfile):
     self.secret = random_string(SECRET_LENGTH)
     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)
Beispiel #3
0
    def __init__(self, url, dirname, urlfile):
        
        Cmd.__init__(self)
        self.secret = GenPassword(SECRET_LENGTH)
        n = Node(url, dirname, self.secret)
        p = Process(target = n._start_server, name = 'rpc-xml_server')
        p.daemon = True
        p.start()
        time.sleep(HEAD_START)

        self.server = ServerProxy(url)
Beispiel #4
0
 def __init__(self, url_file, dir_name, url):  # 定义构造方法
     Cmd.__init__(self)  # 重载超类的构造方法
     self.secret = random_string(SECRET_LENGTH)  # 创建密钥变量
     node = Node(url, dir_name, self.secret)  # 创建节点对象
     thread = Thread(target=node._start)  # 在独立的线程中启动服务器
     thread.setDaemon(True)  # 将线程设置为守护线程
     thread.start()  # 启动线程
     sleep(HEAD_START)  # 等待服务器启动
     self.server = ServerProxy(url)  # 创建服务器代理对象
     for line in open(url_file):  # 读取URL文件
         self.server.hello(line.strip())  # 添加URL文件中的URL到已知节点集合
Beispiel #5
0
 def __init__(self, url, dirname, urlfile):
     Cmd.__init__(self)
     self.secret = randomString(SECRET_LENGTH)
     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 __init__(self, url, dir, urlfile):
     Cmd.__init__(self)
     self.pwd = randomString(PWD_LEN)
     n = Node(url, dir, self.pwd)
     t = Thread(target=n._start)  # 启动线程
     t.setDaemon(1)
     t.start()
     # 启动服务器
     sleep(HEAD_START)
     self.server = ServerProxy(url)
     for line in open(urlfile):  # 每行包括一个已知node的url
         line = line.strip()
         self.server.hello(line)
Beispiel #7
0
    def __init__(self, url, dirname, urlfile):
        '''
        Initialize the node and start the Node server in a seperate thread.
        '''
        Cmd.__init__(self)
        self.secret = randomString(SECRET_LENGTH)
        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):
            self.server.hello(line.strip())
Beispiel #8
0
 def __init__(self, url, dirname, urlfile):
     """
    设置URL、dirname和urlfile,并在一个独立的线程中启动Node服务器
     """
     Cmd.__init__(self)
     self.secret = random_string(SECRET_LENGTH)  # 创建密钥变量
     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):  # 读取URL文件
         line = line.strip()
         self.server.hello(line)  # 添加URL文件中的URL到已知节点集合
Beispiel #9
0
    def __init__(self, url, dirname, urlfile):
        """
		设定url, dirname和urlfile,并且在单独的线程中启动Node服务器
		"""
        Cmd.__init__(self)
        self.secret = randomString(SECRET_LENGTH)
        n = Node(url, dirname, self.secret)
        t = Thread(target=n._start)
        t.setDaemon(1)
        t.start()
        #停0.1秒让服务器先启动
        sleep(HEAD_START)
        self.server = ServerProxy(url)
        for line in open(urlfile, 'r'):
            line = line.strip()
            self.server.hello(line)
Beispiel #10
0
 def __init__(self, url, dirname, urlfile):
     """
     Sets the url, dirname, and urlfile, and starts the Node
     Server in a separate thread.
     """
     Cmd.__init__(self)
     self.secret = randomString(SECRET_LENGTH)
     n = Node(url, dirname, self.secret)
     t = Thread(target=n._start)
     t.setDaemon(1)
     t.start()
     # Give the server a head start:
     sleep(HEAD_START)
     self.server = ServerProxy(url)
     for line in open(urlfile):
         line = line.strip()
         self.server.hello(line)
 def __init__(self, url, dirname, urlfile):
     """
     URL, ディレクトリ名、URLファイルを設定し、
     ノードサーバーを別スレッドで起動する
     """
     Cmd.__init__(self)
     self.secret = random_string(SECRET_LENGTH)
     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 __init__(self, url, dirname, urlfile):
     """
     创建一个随机的密码,使用这个密码实例化Node,
     利用Node的_start方法(确保Thread是个无交互的后台程序,这样它会随着程序的退出而退出)启动THread,
     读取URL文件中的所有URL,并且将Node介绍给这些URL。
     """
     super(Client, self).__init__()
     self.secret = randomString(SECRET_LENGTH)
     n = Node(url, dirname, self.secret)
     t = Thread(target=n._start)
     t.setDaemon(1)  # 设置为True,主线程退出后会杀死子线程
     t.start()
     # 先启动服务器
     sleep(HEAD_START)
     self.server = ServerProxy(url)
     for line in open(urlfile):
         line = line.strip()
         self.server.hello(line)
Beispiel #13
0
 def __init__(self, url, dirname, urlfile):
     """ 设置url、dirname和urlfile,并在一个独立的线程中启动Node服务器  """
     Cmd.__init__(self)
     self.secret = random_string(SECRET_LENGTH)
     n = Node(url, dirname, self.secret)
     t = Thread(target=n._start)
     t.setDaemon(1)
     t.start()
     # 让服务器先行一步:
     sleep(HEAD_START)
     logging.info("  start server  ")
     logging.info(url)
     self.server = ServerProxy(url)
     for line in open(urlfile):
         line = line.strip()
         self.server.hello(line)
         logging.info("  server.hello  ")
         logging.info(line)
Beispiel #14
0
 def __init__(self, url, dirname, urlfile):
     """
     Creates a random secret, instantiates a Node with that secret,
     starts a Thread with the Node's _start method (making sure the
     Thread is a daemon so it will quit when the application quits),
     reads all the URLs from the URL file and introduces the Node to
     them.
     """
     super(Client, self).__init__()
     self.secret = randomString(SECRET_LENGTH)
     n = Node(url, dirname, self.secret)
     t = Thread(target=n._start)
     t.setDaemon(1)
     t.start()
     # Give the server a head start:
     sleep(HEAD_START)
     self.server = ServerProxy(url)
     for line in open(urlfile):
         line = line.strip()
         self.server.hello(line)
Beispiel #15
0
import config
from blockchain_pos import Blockchain
from transaction import TransactionPool
from flask import Flask, jsonify, request, redirect
from wallet import Wallet
from server import Node
import pickle

transactionPool = TransactionPool()
blockchain = Blockchain()

wallet = Wallet("1", "1")
# During initialization, query the node state from peers. If no response, initialize your own state.
node = Node(config.HOST, config.PORT, blockchain)
for p in node.peers:
    outcome = node.init_clone_from_peer(p)
    if outcome:
        blockchain = outcome
        break

app = Flask(__name__)


@app.route('/', methods=['GET'])
def get_node_availability():
    """
    It receives and process messages from user and other nodes
    """
    host_port = node.get_socket()
    response = {'message': f'Node on http://{host_port} is available!'}
    return jsonify(response), 200