分类: Python/Ruby
2017-11-21 18:55:40
一、官网的例子
服务端代码
# Echo server program import socket HOST = '' # Symbolic name meaning all available interfaces PORT = 50007 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept() print 'Connected by', addr while 1: data = conn.recv(1024) if not data: break conn.sendall(data) conn.close()
客户端代码
# Echo client program import socket HOST = '10.118.158.93' # The remote host#改成你自己电脑的IP地址 PORT = 50007 # The same port as used by the server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) s.sendall('Hello, world') data = s.recv(1024) s.close() print 'Received', repr(data)
本机启动服务端代码,本机启动客户端代码,可以看见输出:
Received 'Hello, world' Process finished with exit code 0
本机启动服务端代码,另外一台电脑启动客户端代码,却没有打印
二、原来不是代码的原因,是win7防火墙默认阻止了
1)如下
2)如下
找到python程序,四个地方打上勾,再运行代码,可以看见打印输出了。