- 論壇徽章:
- 2
|
剛開始學(xué)python網(wǎng)絡(luò)編程。在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()
先運(yùn)行server.py,然后運(yùn)行client.py。但是沒有顯示任何結(jié)果。正常應(yīng)該顯示'welcome to server'才對(duì)啊
|
|