Python P2P套接字错误"此操作将永久阻止"

时间:2018-02-07 15:24:05

标签: python sockets p2p python-multithreading gevent

您好我试图创建一个P2P程序来向客户端发送消息。

我的server.py:

import requests
from flask import Flask, jsonify, request
import logging
from daemon import Daemon
import time
import argparse
import sys
from transaction import Transaction
import settings
import socket
import threading

bind_ip = '139.59.26.12'
bind_port = 9998

my_node_addr = str(bind_ip) + ":" + str(bind_port)

clients = [("0.0.0.0", 9999), ("0.0.0.0", 9998)]

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip, bind_port))
server.listen(5)  # max backlog of connections

print('Listening on {}:{}'.format(bind_ip, bind_port))

numbers = []

def broadcast_info(info):
    for n in clients:
        node_str_addr = str(n[0]) + ":" + str(n[1])
        print("Selected: " + node_str_addr)
        if node_str_addr != my_node_addr:
            client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            client.connect((n[0], n[1]))
            client.send(info)
            response = client.recv(4096)

def handle_client_connection(client_socket):
    request = client_socket.recv(1024)
    request_str = request.decode()
    if request_str == 'print':
        client_socket.send(str(numbers).encode('utf-8'))
        print(numbers)
    else:
        client_socket.send('Added!'.encode('utf-8'))
        client_socket.close()
        num_rec = int(request.decode())
        if num_rec not in numbers:
            numbers.append(num_rec)
            #broadcast_info(request)

while True:
    client_sock, address = server.accept()
    print('Accepted connection from {}:{}'.format(address[0], address[1]))
    client_handler = threading.Thread(
        target=handle_client_connection,
        args=(client_sock,)
        # without comma you'd get a... TypeError: handle_client_connection() argument after * must be a sequence, not _socketobject
    )
    client_handler.start()

我的客户:

import socket

hostname, sld, tld, port = 'www', 'integralist', 'co.uk', 80
target = '{}.{}.{}'.format(hostname, sld, tld)

# create an ipv4 (AF_INET) socket object using the tcp protocol (SOCK_STREAM)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# connect the client
# client.connect((target, port))
client.connect(('139.59.76.32', 9998))

# send some data (in this case a HTTP GET request)
client.send("2".encode('utf-8'))  # Number or "print"

# receive the response data (4096 is recommended buffer size)
response = client.recv(4096)

print (response)

client.close()

有时效果很好。但是经过几次尝试后出现了这个错误:

Exception in thread Thread-11:
Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "server.py", line 43, in handle_client_connection
    request = client_socket.recv(1024)
  File "/usr/local/lib/python3.5/dist-packages/gevent/_socket3.py", line 336, in recv
    self._wait(self._read_event)
  File "/usr/local/lib/python3.5/dist-packages/gevent/_socket3.py", line 156, in _wait
    self.hub.wait(watcher)
  File "/usr/local/lib/python3.5/dist-packages/gevent/hub.py", line 651, in wait
    result = waiter.get()
  File "/usr/local/lib/python3.5/dist-packages/gevent/hub.py", line 898, in get
    return self.hub.switch()
  File "/usr/local/lib/python3.5/dist-packages/gevent/hub.py", line 630, in switch
    return RawGreenlet.switch(self)
gevent.hub.LoopExit: ('This operation would block forever', <Hub at 0x7fdf21aa2af8 epoll pending=0 ref=0 fileno=9>)

我尝试了一切。在我的本地机器工作正常。但是当我尝试在我的Digitalocean VPS上执行此操作时,就会发生这种情况。我通过互联网搜索此错误,但我无法找到任何可以帮助我的内容。

你知道这个错误的原因是什么吗?

由于

0 个答案:

没有答案