How to solve socket.error: [Errno 10061] in Python with server and my computer
How to solve socket.error: [Errno 10061] in Python with server and my computer
我想在我的计算机和服务器 (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]
怎么了?我该怎么办?
求助。
您在服务器中的这一行有错误:
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)
监听所有活动接口。
我想在我的计算机和服务器 (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]
怎么了?我该怎么办?
求助。
您在服务器中的这一行有错误:
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)
监听所有活动接口。