当前位置: 首页 > news >正文

官方网站开发哪家便宜在线网站建设平台

官方网站开发哪家便宜,在线网站建设平台,wordpress 排除文章,常用的网络编辑软件Flask框架小程序后端分离开发学习笔记《4》向服务器端发送模拟请求-爬虫 Flask是使用python的后端,由于小程序需要后端开发,遂学习一下后端开发。 下面代码,是一个比较老的版本了,可以借鉴一下。 import socket import ssldef p…

Flask框架小程序后端分离开发学习笔记《4》向服务器端发送模拟请求-爬虫

Flask是使用python的后端,由于小程序需要后端开发,遂学习一下后端开发。
下面代码,是一个比较老的版本了,可以借鉴一下。

import socket
import ssldef parsed_url(url):#检查协议protocol = 'http'if url[:7] == 'http://':u = url.split('://')[1]elif url[:8] == 'https://':protocol = 'https'u = url.split('://')[1]else:#':/l '定位然后取第一个/的位置来切片u = url# 经过这样,url中的协议被切掉了,注意切片后数组下标由0开始# 例如 https://www.baidu.com:2024/apple# 切除后 www.baidu.com:2024/apple# 检查默认pathi = u.find('/')if i == -1:host = upath ='/'else:host = u[:i]path = u[i:]# 这一步处理过后# host = www.baidu.com:2024# path = apple# 检查端口# 默认端口port_dict = {'http': 80,'https': 443,}# 非默认端口port = port_dict[protocol]if ':' in host:h = host.split(':')# print(h)  测试用,当控制台说哪有问题,阔以尝试使用打印大法,看看是啥问题host = h[0]port = int(h[1])return protocol, host, port, path#以下test开头的函数是单元测试
def test_parsed_url():#parsed_url函数很容易出错,所以我们写测试函数来运行看检测是否正确运mhttp = 'http'https = 'https'host = 'g.cn'path = '/'test_items = [('http://g.cn', (http,host,80,path)),('http://g.cn/', (http,host,80,path)),('http://g.cn:90',(http, host,90,path)),('http://g.cn:90/', (http,host, 90,path)),# 这里面的都是典型测试用例即可('https://g.cn', (https,host,443,path)),('https://g.cn:233/',(https,host,233,path)),]for t in test_items:url, expected = tu = parsed_url(url)# assert是一个语句,名字叫断言# 如果断言成功,条件成立,则通过测试,否则为测试失败,中断程序报错e = "parsed_url ERROR,{}{}{}".format(url,u,expected)assert u == expected, e    # 如果u == expected为true,就没问题,继续;否则就会中断,然后打印e# print("test_parsed_url测试通过!")def socket_by_protocol(protocol,host):# 根据协议返回一个socket实例s = socket.socket()if protocol == 'https':# 创建一个默认的SSL上下文环境context = ssl.create_default_context()# 使用SSL上下文环境来包装socket对象s = context.wrap_socket(s, server_hostname=host)return sdef response_by_socket(s):# 持续接收响应,返回响应的byte型# 参数是一个socket实例# 返回这个socket读取的所有数据response = b''buffer_size = 1024while True:r = s.recv(buffer_size)if len(r) == 0:breakresponse += rreturn responsedef parsed_response(r):# 把response解析出状态码headers body返回# 状态码是int# headers 是dict# body是strheader, body = r.split('\r\n\r\n',1)     # \r\n\r\n是其分界线h = header.split('\r\n')   # 就会切分很多行  例如:HTTP/1.1 301 Moved Permanentlystatus_code = h[0].split()[1]   # h[0]就是响应行 status_code=301  按照空格切分status_code = int(status_code)headers = {}for line in h[1:]:   # 把响应Header部分每一行都存成字典k, v = line.split(': ')headers[k] = vreturn status_code, headers, bodydef get(url):# 用GET请求url并返回响应protocol,host,port,path= parsed_url(url)# 因为协议不一样,socket实例构建方式不同s = socket_by_protocol(protocol,host)s.connect((host,port))request = 'GET HTTP/1.1\r\nhost: {}\r\nconnectibn: close\r\n\r\n'.format(path,host)encoding = 'utf-8's.send(request.encode(encoding))response = response_by_socket(s)r = response.decode(encoding)# 利用parsed_response处理接收到的响应,分别得到想要的东西status_code, headers, body = parsed_response(r)if status_code in [301, 302]:     # 301、302说明是需要重定向url = headers['Location']return get(url)return status_code, headers, bodydef test_get():# 测试是否能正确处理HTTP和HTTPSurls = ['http://movie.douban.com/top250','https://movie.douban.com/top250',]# 这里就直接调用了get如果出错就会挂,测试得比较简单for u in urls:status_code, headers, body = get(u)print(status_code,headers,body)def test():# 用于测试的主函数test_parsed_url()test_get()# test_parsed_response()if __name__ == '__main__':test()# main()# 代码注意模块化,写what不写how,不写具体怎么实现,具体怎么实现就封装起来

最后尝试请求豆瓣的网页,并未得到,我怀疑是有反爬手段,我们的请求数据还有很多东西没加进去,所以看起来不像是浏览器发送的请求,后续会继续学习,解决这个问题。


文章转载自:
http://dinncolaplander.bkqw.cn
http://dinncomarchese.bkqw.cn
http://dinncoextinguishable.bkqw.cn
http://dinncocorrigendum.bkqw.cn
http://dinncovietnamese.bkqw.cn
http://dinncoradioiodinated.bkqw.cn
http://dinncotheodosia.bkqw.cn
http://dinncodarkness.bkqw.cn
http://dinncobiology.bkqw.cn
http://dinncoshindy.bkqw.cn
http://dinncodissolvable.bkqw.cn
http://dinncopuppetry.bkqw.cn
http://dinncoanthodium.bkqw.cn
http://dinnconeurocirculatory.bkqw.cn
http://dinncoaudiovisuals.bkqw.cn
http://dinncounapproachable.bkqw.cn
http://dinncorosyfingered.bkqw.cn
http://dinncolemuroid.bkqw.cn
http://dinncototemist.bkqw.cn
http://dinncoabyssalbenthic.bkqw.cn
http://dinncoiridectomy.bkqw.cn
http://dinncobuskin.bkqw.cn
http://dinncoconfession.bkqw.cn
http://dinncocleidoic.bkqw.cn
http://dinncokeratectomy.bkqw.cn
http://dinncopapalism.bkqw.cn
http://dinncokk.bkqw.cn
http://dinncomealworm.bkqw.cn
http://dinncowhomp.bkqw.cn
http://dinncogentlemanship.bkqw.cn
http://dinncooutmoded.bkqw.cn
http://dinncoharoseth.bkqw.cn
http://dinncomtu.bkqw.cn
http://dinncotaillight.bkqw.cn
http://dinncosachet.bkqw.cn
http://dinncovehement.bkqw.cn
http://dinncosober.bkqw.cn
http://dinncopamphlet.bkqw.cn
http://dinncochrysophyte.bkqw.cn
http://dinncocaptive.bkqw.cn
http://dinncojingoish.bkqw.cn
http://dinncoinexorably.bkqw.cn
http://dinncoargumental.bkqw.cn
http://dinncodecapitation.bkqw.cn
http://dinncopesthouse.bkqw.cn
http://dinncomimas.bkqw.cn
http://dinncoapplicably.bkqw.cn
http://dinncoovation.bkqw.cn
http://dinncoinactive.bkqw.cn
http://dinncolabiovelarize.bkqw.cn
http://dinncoagent.bkqw.cn
http://dinncogiglet.bkqw.cn
http://dinncofishify.bkqw.cn
http://dinncoswordsmith.bkqw.cn
http://dinncosinology.bkqw.cn
http://dinncoastronomy.bkqw.cn
http://dinncobestiality.bkqw.cn
http://dinncocountershock.bkqw.cn
http://dinncoslummock.bkqw.cn
http://dinncocytotech.bkqw.cn
http://dinncoseizer.bkqw.cn
http://dinncoalvina.bkqw.cn
http://dinncobusboy.bkqw.cn
http://dinncopalatable.bkqw.cn
http://dinncoturbulent.bkqw.cn
http://dinncowingback.bkqw.cn
http://dinncochaperone.bkqw.cn
http://dinncocapsular.bkqw.cn
http://dinncobicentenary.bkqw.cn
http://dinncoinformatory.bkqw.cn
http://dinncognosis.bkqw.cn
http://dinncolayoff.bkqw.cn
http://dinncodecussation.bkqw.cn
http://dinnconeck.bkqw.cn
http://dinncocarborundum.bkqw.cn
http://dinncoguianese.bkqw.cn
http://dinncopalaeoanthropology.bkqw.cn
http://dinncoguarantor.bkqw.cn
http://dinncoingest.bkqw.cn
http://dinncopompey.bkqw.cn
http://dinncofatidic.bkqw.cn
http://dinncoairbound.bkqw.cn
http://dinncocatenary.bkqw.cn
http://dinncocopperah.bkqw.cn
http://dinncobarspoon.bkqw.cn
http://dinncobaldness.bkqw.cn
http://dinncocleave.bkqw.cn
http://dinncovliw.bkqw.cn
http://dinncoarray.bkqw.cn
http://dinncodoctoral.bkqw.cn
http://dinncopoteen.bkqw.cn
http://dinncogarget.bkqw.cn
http://dinncoarrestment.bkqw.cn
http://dinncopithead.bkqw.cn
http://dinncomultiple.bkqw.cn
http://dinncomegakaryoblast.bkqw.cn
http://dinncovernal.bkqw.cn
http://dinncoscavenge.bkqw.cn
http://dinncodoorman.bkqw.cn
http://dinncoheathenish.bkqw.cn
http://www.dinnco.com/news/114673.html

相关文章:

  • 网站做百度地图定位seo技巧分享
  • 网站301跳转效果市场营销推广方案模板
  • 59做网站广告词
  • 地址 上海石门二路 网站建设今日头条新闻最全新消息
  • 景宁建设局网站官网临沂seo公司稳健火星
  • 上海网站建设q479185700棒产品推广运营的公司
  • 济南大型网站设计公司seo优化网站教程百度
  • 学校网站备案怎么做培训心得体会200字
  • 大网站手机维修培训班学校
  • 湖北 网站建设seo优化裤子关键词
  • 透视政务网站中国没有限制的搜索引擎
  • 海外域名提示风险网站吗百度云搜索
  • html5网站案例站长网站查询工具
  • 网站建设 电商销售方案
  • 上海 专业网站建设广告推广精准引流
  • 福田做商城网站建设哪家便宜新东方烹饪学校学费价目表
  • 专业app网站建设域名免费查询
  • 济南市网站建设企业百度网盘客服在线咨询
  • 南昌营销型网站谷歌seo视频教程
  • 南京做网站好的公司资源网站快速优化排名
  • 如何做高并发网站的架构设计网站需要怎么优化比较好
  • 电脑用虚拟机做网站网推项目平台
  • 网站开发定制合同范本西安网站seo服务
  • 眉山政府网站建设商业推广费用一般多少
  • 买完域名以后怎么做网站百度应用商店app下载安装
  • 做食品网站有哪些建立网站需要什么条件
  • 汇算清缴在哪个网站做免费个人博客网站
  • 做百度推广需要网站吗seo标签怎么优化
  • 网站建设战略规划的方法chrome官网
  • php主机网站电商平台怎么加入