python-socket

服务端:

>>>
接收到连接 ('127.0.0.1', 55945)
服务端接收: 'hello'
服务端接收: 'deepfuture.iteye.com'

 

 

 

客户端:

>>>
客户端接收: 'server send:hello'
客户端接收: 'server send:deepfuture.iteye.com'
>>>

 

windows环境:ctrl+alt+delete 终止

linux/unix:ctrl+c终止

 

服务端

#!/usr/bin/env python  
# -*- coding: utf-8 -*-

from socket import *
myhost='127.0.0.1'
myport=50009

sockobj=socket(AF_INET,SOCK_STREAM)
sockobj.bind((myhost,myport))
sockobj.listen(5)#监听并接收5个连接

while True:
    connection,address=sockobj.accept()
    print '接收到连接',address
    while True:
        data=connection.recv(1024)
        if not data:
            break
        print '服务端接收:',repr(data)        
        connection.send('server send:'+data)
    connection.close()
    
        
        
    


 

客户端

 

#!/usr/bin/env python  
# -*- coding: utf-8 -*-

import sys
from socket import *
serverhost='127.0.0.1'
serverport=50009

sockobj=socket(AF_INET,SOCK_STREAM)
sockobj.connect((serverhost,serverport))
mesage=['hello','deepfuture.iteye.com']
for line in mesage:
    sockobj.send(line)
    data=sockobj.recv(1024)
    print '客户端接收:',repr(data)
sockobj.close()




 

你可能感兴趣的:(python)