server.bind(),只有本地IP地址

时间:2015-12-11 12:02:34

标签: python sockets

我无法使用我的计算机的真实IP地址定义服务器,它给我一个错误。当我使用本地IP地址执行此操作时,它可以工作,但我想在其他计算机上运行客户端

import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(('my_ip',9876))
server_socket.listen(1)
(client_socket,client_address)=server_socket.accept()
recived_data=client_socket.recv(999999999)
password=open(r'D:\passwords','wb')
password.write(recived_data)
client_socket.close()
server_socket.close()

客户:

import socket
my_socket = socket.socket()
my_socket.connect(('the_real_ip', 9876))

我得到的错误是:

Traceback (most recent call last):
File "D:/Heights/Documents/Projects/Cyber/Password_Server.py", line 4, in <module>
server_socket.bind(('my_ip',9876))
File "D:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 10049] The requested address is not valid in its context

1 个答案:

答案 0 :(得分:1)

您可以使用server_socket.bind(('0.0.0.0',9876))server_socket.bind(('',9876))

点击此链接:https://serverfault.com/questions/78048/whats-the-difference-between-ip-address-0-0-0-0-and-127-0-0-1

要从局域网外部进行连接,您必须使用“外部IP”连接到该局域网的路由器,并使路由器成为与“本地IP”的连接。