Chinaunix首页 | 论坛 | 博客
  • 博客访问: 157232
  • 博文数量: 33
  • 博客积分: 2057
  • 博客等级: 大尉
  • 技术积分: 430
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-19 16:37
文章分类
文章存档

2013年(2)

2012年(23)

2011年(8)

分类: Python/Ruby

2012-10-10 09:09:48


点击(此处)折叠或打开

  1. import socket
  2. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  3. port = input("port: ")
  4. sock.bind(('localhost', port))
  5. print "serving on port %s" %(port)
  6. sock.listen(1)

  7. while 1:
  8.     connection,address = sock.accept()
  9.     while 1:
  10.         print "receiving ..."
  11.         buf = connection.recv(1024)
  12.         if buf == '1': connection.send('welcome to server!')
  13.         elif buf == 'shutdown': connection.close();break
  14.         else: connection.send(buf);print buf

点击(此处)折叠或打开

  1. import socket
  2. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  3. port = input("port: ")
  4. sock.connect(('localhost', port))
  5. while 1:
  6.     data = raw_input("data to send: ")
  7.     if data == "quit":
  8.         sock.close()
  9.         break
  10.     sock.send(data)
  11.     print "receiving..."
  12.     datas = sock.recv(1024)
  13.     print datas

阅读(815) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~