python网络编程
想使用python体验下多简单,于是写了最简单的一个C/S程序,的确实现起来很简单!
服务器端程序:
#!/usr/bin/python
import socket
listensock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
listensock.bind(('192.168.0.172',6000))
listensock.listen(50)
while 1:
newconnsock,address=listensock.accept()
print 'got connected from ',address
newconnsock.send('hello i am server,welcome to connect me')
ra=newconnsock.recv(512)
print ra
newconnsock.close()
print 'server closed the new connection'
客户端程序:
#!/usr/bin/python
import socket
s=socket.socket()
s.connect(('192.168.0.172',6000))
data=s.recv(512)
print 'get the server data is ',data
s.send('hi,i am client,i request to connect server!/n')
print 'client sent hihi to server!/n'
s.close()
print 'client closed socket/n'
我在同一台机器上运行成功,但是当我使用另外一台计算机作为客户端时,每次运行客户端程序都失败,
错误是:socket.error : (113 , 'No route to host '),检查发现端口没有问题,原来我的iptables开
着呢,把服务器端的防火墙关闭后,连接成功,okey!