Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3972800
  • 博文数量: 366
  • 博客积分: 9916
  • 博客等级: 中将
  • 技术积分: 7195
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-29 23:27
个人简介

简单!

文章分类

全部博文(366)

文章存档

2013年(51)

2012年(269)

2011年(46)

分类: Python/Ruby

2012-04-02 14:31:10

因工作需求,接触了一下python,感觉跟c差不多,尝试写了个小程序。

客户端:

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. #encoding = utf-8

  3. import socket

  4. s = socket.socket()
  5. server = socket.gethostname()
  6. print server

  7. port = 8888
  8. s.connect((server,port))
  9. print s.recv(1024)

  10. while True:
  11.     say = raw_input(u'client:\n '.encode('gbk'))  //使用raw_input()函数取得用户输入的数据
  12.     s.send(str(say))
  13.     print u'server'
  14.     print ' %s'%s.recv(1024)

  15. s.close()
  16. raw_input()

服务器:

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. #encoding = utf-8

  3. import socket

  4. s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

  5. host = ''
  6. port = 8888
  7. s.bind((host,port))

  8. s.listen(10)

  9. c,addr = s.accept()
  10. print 'get connection from',addr
  11. c.send('this is a simple server')

  12. while True:
  13.     print u'client: '
  14.     print ' %s'%c.recv(1024)
  15.     ssay = raw_input(u'server:\n '.encode('gbk'))
  16.     c.send(str(ssay))

  17. c.close()
  18. raw_input()
阅读(1457) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~