文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>socket编程1 [Python]

socket编程1 [Python]

时间:2008-10-03  来源:lvDbing

服务器端: echo-server.py  

[root@lvdbing socket]# cat echo-server.py
#!/usr/bin/env python

from socket import *

myHost = ''
myPort = 12345

socketobj = socket(AF_INET,SOCK_STREAM)
socketobj.bind((myHost,myPort))
socketobj.listen(5)

while True:
        connection, address = socketobj.accept()
        print 'Server connected by', address
        while True:
                data = connection.recv(1024)
                if not data: break
                connection.send('Echo=>' + data)
        connection.close()

 

客户端: echo-client.py

[root@lvdbing socket]# cat echo-client.py
#!/usr/bin/env python

import sys
from socket import *

serverHost = 'localhost'
serverPort = 12345

message = ['Welcome to lvDbing blog']
if len(sys.argv) > 1:
        serverHost = sys.argv[1]

if len(sys.argv) > 2:
        message = sys.argv[2:]

socketobj = socket(AF_INET, SOCK_STREAM)
socketobj.connect((serverHost, serverPort))

for line in message:
        socketobj.send(line)
        data = socketobj.recv(1024)
        print 'Client received:', repr(data)

socketobj.close()

相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载