首页 技术 正文
技术 2022年11月21日
0 收藏 416 点赞 2,546 浏览 1771 个字

一、异步爬网页

'''
协程并发爬网页
'''
from urllib import request
import gevent,time
from gevent import monkey # 让gevent知道urllib里的哪些操作属于IO操作monkey.patch_all() # 标记当前程序所有的IO操作def f(url):
print("GET:{0}".format(url))
resp = request.urlopen(url)
data = resp.read()
print('{0} bytes received from {1}'.format(len(data), url))time_start = time.time()
f("https://www.python.org")
f("https://www.yahoo.com")
f("https://github.com")
print("同步耗时:{0}".format(time.time()-time_start))async_time_start = time.time()
gevent.joinall([gevent.spawn(f,"https://www.python.org"),
gevent.spawn(f, "https://www.yahoo.com"),
gevent.spawn(f, "https://github.com")])
print("异步耗时:{0}".format(time.time()-async_time_start))'''
GET:https://www.python.org
49060 bytes received from https://www.python.org
GET:https://www.yahoo.com
498196 bytes received from https://www.yahoo.com
GET:https://github.com
64978 bytes received from https://github.com
同步耗时:10.61960744857788
GET:https://www.python.org
GET:https://www.yahoo.com
GET:https://github.com
86167 bytes received from https://github.com
49060 bytes received from https://www.python.org
503102 bytes received from https://www.yahoo.com
异步耗时:3.7582149505615234
'''

二、异步处理socket请求

服务端:

import socket
import gevent
from gevent import monkeymonkey.patch_all()def server(port):
s = socket.socket()
s.bind(('0.0.0.0', port))
s.listen(500)
while True:
cli, addr = s.accept() # 之前多并发是启动一个线程
gevent.spawn(handle_request, cli)def handle_request(conn):
try:
while True:
data = conn.recv(1024)
print("recv:", data)
conn.send(data)
if not data:
conn.shutdown(socket.SHUT_WR)
except Exception as ex:
print(ex)
finally:
conn.close()if __name__ == '__main__':
server(8001)

客户端:

import socketHOST = 'localhost'
PORT = 8001s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
while True:
msg = bytes(input(">>").strip(), encoding="utf-8")
s.sendall(msg)
data = s.recv(1024)
print("Received:", repr(data))
s.close()
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919