Code
#server.py
import socket
s1=socket.socket();
s1.bind(('',65533))
s1.listen(5)
while 1:
sc,add=s1.accept()
print 'add is :',add
print 'content is :',sc.recv(1024)
sc.send("welcome")
#client.py
import socket
sc=socket.socket()
sc.connect(('localhost',65533))
sc.send('bullshitWorld')
print sc.recv(1024)
sc.close()
#example from internet
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost', 8001))
sock.listen(5)
while True:
connection,address = sock.accept()
try:
connection.settimeout(5)
buf = connection.recv(1024)
if buf == '1':
connection.send('welcome to server!')
else:
connection.send('please go out!')
except socket.timeout:
print 'time out'
connection.close()
posted @
2009-01-08 10:49
Dannis wu
阅读(
192)
评论()
编辑
收藏
举报