如何使用服务器和我的计算机在Python中解决socket.error:[Errno 10061]

时间:2016-11-19 06:42:57

标签: python sockets tcp communication

我想在我的计算机和服务器(ubuntu)之间进行TCP通信。

服务器IP为203.246.114.176,其端口30000已打开。

服务器正在运行以下server.py:

import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('localhost', 30000)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

while True:
    # Wait for a connection
    print >>sys.stderr, 'waiting for a connection'
    connection, client_address = sock.accept()

    try:
        print >>sys.stderr, 'connection from', client_address

        # Receive the data in small chunks and retransmit it
        while True:
            data = connection.recv(16)
            print >>sys.stderr, 'received "%s"' % data
            if data:
                print >>sys.stderr, 'sending data back to the client'
                connection.sendall(data)
            else:
                print >>sys.stderr, 'no more data from', client_address
                break

    finally:
        # Clean up the connection
        connection.close()

我的电脑有client.py:

import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = ('203.246.114.176', 30000)
print >>sys.stderr, 'connecting to %s port %s' % server_address
sock.connect(server_address)

try:

    # Send data
    message = 'This is the message.  It will be repeated.'
    print >>sys.stderr, 'sending "%s"' % message
    sock.sendall(message)

    # Look for the response
    amount_received = 0
    amount_expected = len(message)

    while amount_received < amount_expected:
        data = sock.recv(16)
        amount_received += len(data)
        print >>sys.stderr, 'received "%s"' % data

finally:
    print >>sys.stderr, 'closing socket'
    sock.close()

当我在服务器上运行时,输出如下:

starting up on localhost port 30000
waiting for a connection

,ufw状态如下:

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
5979/tcp                   ALLOW       Anywhere
5901                       ALLOW       Anywhere
8080                       ALLOW       Anywhere
5978/tcp                   ALLOW       Anywhere
5980/tcp                   ALLOW       Anywhere
31415                      ALLOW       Anywhere
5981                       ALLOW       Anywhere
5982                       ALLOW       Anywhere
5983                       ALLOW       Anywhere
5984                       ALLOW       Anywhere
5985                       ALLOW       Anywhere
22/tcp                     ALLOW       Anywhere
2222/tcp                   ALLOW       Anywhere
77/tcp                     ALLOW       Anywhere
21                         ALLOW       Anywhere
3306                       ALLOW       Anywhere
30000/tcp                  ALLOW       Anywhere
30001/tcp                  ALLOW       Anywhere
30002/tcp                  ALLOW       Anywhere
30003/tcp                  ALLOW       Anywhere
30004/tcp                  ALLOW       Anywhere
30005/tcp                  ALLOW       Anywhere
30006/tcp                  ALLOW       Anywhere
30007/tcp                  ALLOW       Anywhere
22 (v6)                    ALLOW       Anywhere (v6)
5979/tcp (v6)              ALLOW       Anywhere (v6)
5901 (v6)                  ALLOW       Anywhere (v6)
8080 (v6)                  ALLOW       Anywhere (v6)
5978/tcp (v6)              ALLOW       Anywhere (v6)
5980/tcp (v6)              ALLOW       Anywhere (v6)
31415 (v6)                 ALLOW       Anywhere (v6)
5981 (v6)                  ALLOW       Anywhere (v6)
5982 (v6)                  ALLOW       Anywhere (v6)
5983 (v6)                  ALLOW       Anywhere (v6)
5984 (v6)                  ALLOW       Anywhere (v6)
5985 (v6)                  ALLOW       Anywhere (v6)
22/tcp (v6)                ALLOW       Anywhere (v6)
2222/tcp (v6)              ALLOW       Anywhere (v6)
77/tcp (v6)                ALLOW       Anywhere (v6)
21 (v6)                    ALLOW       Anywhere (v6)
3306 (v6)                  ALLOW       Anywhere (v6)
30000/tcp (v6)             ALLOW       Anywhere (v6)
30001/tcp (v6)             ALLOW       Anywhere (v6)
30002/tcp (v6)             ALLOW       Anywhere (v6)
30003/tcp (v6)             ALLOW       Anywhere (v6)
30004/tcp (v6)             ALLOW       Anywhere (v6)
30005/tcp (v6)             ALLOW       Anywhere (v6)
30006/tcp (v6)             ALLOW       Anywhere (v6)
30007/tcp (v6)             ALLOW       Anywhere (v6)

但是我的电脑显示了这个输出:

C:\Users\user\Documents\notepad>python client.py
Traceback (most recent call last):
  File "client.py", line 7, in <module>
    s.connect((host, port))
  File "C:\Python27\lib\socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 10061]

怎么回事?我该怎么办?

帮助PLZ。

1 个答案:

答案 0 :(得分:1)

此行中的服务器出错:

server_address = ('localhost', 30000)

localhost通常为127.0.0.1,因此您没有按照您的想法将其约束到203.246.114.176。尝试使用

server_address = ('203.246.114.176', 30000)

获取此确切的IP地址,或

server_address = ('0.0.0.0', 30000)

监听所有活动接口。