Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1784378
  • 博文数量: 276
  • 博客积分: 1574
  • 博客等级: 上尉
  • 技术积分: 2894
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-26 23:23
个人简介

生活的美妙在于,不知道一下秒是惊艳还是伤神,时光流转,珍惜现在的拥有的时光

文章分类

全部博文(276)

文章存档

2017年(17)

2016年(131)

2015年(63)

2013年(2)

2012年(32)

2011年(31)

分类: Python/Ruby

2016-05-09 16:55:44

客户端socket

点击(此处)折叠或打开

  1. #!/usr/bin/env python3
  2. #-*- coding:utf-8 -*-
  3. '''
  4. '''
  5. #导入socket网络编程模块
  6. import socket
  7. #创建客户端通信对象
  8. client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  9. #建立客户端与服务器连接,ip+port以数组的方式传入
  10. client_socket.connect(('', 80))
  11. #执行客户端操作
  12. client_socket.send(b'GET / HTTP/1.1\r\nHost: \r\nConnection: close\r\n\r\n')

  13. #接收数据
  14. buffer=[]
  15. while True:
  16.     datarecv=client_socket.recv(1024)
  17.     if datarecv:
  18.         buffer.append(datarecv)
  19.     else:
  20.         break
  21. data = b''.join(buffer)
  22. #关闭链接
  23. client_socket.close()

  24. header, html = data.split(b'\r\n\r\n',1)
  25. print(header.decode('utf-8'))
  26. #数据写入文件
  27. with open('sina.html', 'wb') as f:
  28.     f.write(html)


t@localhost untitled$ python3 socket_client.py 
HTTP/1.1 200 OK
Content-Type: text/html
Vary: Accept-Encoding
X-Powered-By: schi_v1.02
Server: nginx
Date: Mon, 09 May 2016 08:26:19 GMT
Last-Modified: Mon, 09 May 2016 08:24:36 GMT
Expires: Mon, 09 May 2016 08:27:19 GMT
Cache-Control: max-age=60
Age: 48
Content-Length: 549273
X-Cache: HIT from localhost
Connection: close

服务端socket
服务端

点击(此处)折叠或打开

  1. #!/usr/bin/env python3
  2. #-*- coding:utf-8 -*-
  3. '''
  4. '''
  5. #导入库
  6. import socket, threading, time
  7. #创建socket实例
  8. server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  9. #绑定监听网卡
  10. server_socket.bind(('0.0.0.0', 10086))
  11. #监听连接,最大连接数5
  12. server_socket.listen(5)
  13. print('Wait for connection...')
  14. #定义处理函数
  15. def tcplink(sock, addr):
  16.     print('sock:',type(sock))
        print('addr:',type(addr))
  17.     print('Accept new connection from %s:%s' % addr)
  18.     sock.send(b'Welcome!')
  19.     while True:
  20.         data = sock.recv(1024)
  21.         time.sleep(1)
  22.         if not data or data.decode(('utf-8')) == 'exit':
  23.             break
  24.         sock.send(('Hello, %s!' % data.decode('utf-8')).encode('utf-8'))
  25.     sock.close()
  26.     print('Connection from %s:%s closed.' % addr )
  27. #创建永久循环来接受客户端连接
  28. while True:
  29. #接受一个新连接
  30.     sock, addr = server_socket.accept()
  31. #创建一个线程处理请求
  32.     t = threading.Thread(target=tcplink, args=(sock, addr))
  33.     t.start()
客户端

点击(此处)折叠或打开

  1. #!/usr/bin/env python3
  2. #-*- coding:utf-8 -*-
  3. '''
  4. '''


  5. import socket
  6. socket_client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  7. socket_client.connect(('127.0.0.1',10086))
  8. print(socket_client.recv(1024).decode('utf-8'))
  9. for data in [b'talen',b'eric',b'tom']:
  10.     socket_client.send(data)
  11.     print(socket_client.recv(1024).decode('utf-8'))
  12. socket_client.send(b'exit')
  13. socket_client.close()
print('the connection from %s:%s is closed' % addr)   # addr是一个tuple(IP,port),需要两个%s来接受数据。the connection from 127.0.0.1:20481 is closed














t@localhost untitled$ python3 socket_client2.py 
Welcome!
Hello, talen!
Hello, eric!
Hello, tom!


参考学习:
阅读(1190) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~