- 论坛徽章:
- 2
|
刚开始学python网络编程。在windows上分别建立server.py和client.py文件
client.py:
import socket
import time
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect(('192.168.1.107',8001))
time.sleep(2)
sock.send('1')
sock.close()
server.py
import socket
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.bind(('192.168.1.107',8001))
sock.listen(5)
while True:
connection,address=sock.accept()
try:
connection.settimeout(5)
buf=connection.recv(1024)
print buf
if buf == '1':
print 'reveived data'
connection.send('welcome to server')
else:
connection.send('Please go out!')
except socket.timeout:
print 'time out'
connection.close()
先运行server.py,然后运行client.py。但是没有显示任何结果。正常应该显示'welcome to server'才对啊
|
|