【python3 获取http请求的返回状态码】教程文章相关的互联网学习教程文章

python3 获取http请求的返回状态码

#!/bin/env python # -*- coding: UTF-8 -*- # example: python3 http_status.py https://blog.51cto.com/liuxiaolan/2488619 import requests import sysdef http_status(arg):try:html = requests.get(arg)code = html.status_codeprint(code)except:print(1)sys.exit(0)if __name__ == "__main__":code = sys.argv[1]http_status(code)原文:https://blog.51cto.com/liuxiaolan/2490018

Python数据库访问公共组件及模拟Http请求【代码】

前言  最近一段时间除了忙于工作之外,在业余时,迷上了python,对它的跨平台深深的吸引。经过一段时间的自我学习,了解了其基本的语法,便开始自己着手摆弄起来。主要想把以前对接的接口再实现一次,以便于在做中发现问题,解决问题。只看不做,没有实际的操练,永远都是纸上谈兵。在此过程中遇到了许多问题,通过不断查询资料和请教基本完善了功能。现将自我觉得比较重要的部分拿出来和大家一起探讨一下,也顺便自己对此做个记...

Python + requests 发送HTTP请求【代码】

一、在接口自动化测试过程中,存在两种情况:  一种是不需要鉴权的接口,可以直接访问的。  还有一种情况是需要鉴权才可以访问的接口。这里我们通过Python + requests 实现这两种发送请求的方法"""============================author:Treasure丶time:2019/11/28E-mail:1247814617@qq.com发送Http请求的方法============================"""import requestsclass HttpRequest(object): """不记录任何的请求方法""" @clas...

【Python】python http 请求【代码】

python2.7 版# coding=UTF-8 # urllib2_get.pyimport urllib2 import StringIO import gzipurl = 'http://wthrcdn.etouch.cn/weather_mini?city=杭州'response = urllib2.urlopen(url).read()data = StringIO.StringIO(response) gzipper = gzip.GzipFile(fileobj=data) html = gzipper.read()print html python3.6 版import json import requests import sysif len(sys.argv) < 2:print('Please city params !')sys.exit()# 从命令...

HTTP请求的python实现(urlopen、headers处理、 Cookie处理、设置Timeout超时、 重定向、Proxy的设置)【代码】【图】

## python实现HTTP请求的三中方式:urllib2/urllib、httplib/urllib 以及Requestsurllib2/urllib实现urllib2和urllib是python两个内置的模块,要实现HTTP功能,实现方式是以urllib2为主,urllib为辅1 首先实现一个完整的请求与响应模型urllib2提供基础函数urlopen,import urllib2 response = urllib2.urlopen(‘http://www.cnblogs.com/guguobao‘) html = response.read() print html改进,分两步:请求和响应#!coding:utf-8 imp...

基于http请求与响应实现的网页源码读取的相关操作技巧【图】

这篇文章主要介绍了Python实现的下载网页源码功能,涉及Python基于http请求与响应实现的网页源码读取功能相关操作技巧,需要的朋友可以参考下本文实例讲述了Python实现的下载网页源码功能。分享给大家供大家参考,具体如下:#!/usr/bin/python import httplib httpconn = httplib.HTTPConnection("www.baidu.com") httpconn.request("GET", "/index.html") resp = httpconn.getresponse() if resp.reason == "OK":resp_data = resp.r...

Python中使用socket发送HTTP请求数据接收不完整问题解决方法

由于工作的需求,需要用python做一个类似网络爬虫的采集器。虽然Python的urllib模块提供更加方便简洁操作,但是涉及到一些底层的需求,如手动设定User-Agent,Referer等,所以选择了直接用socket进行设计。当然,这样的话,需要对HTTP协议比较熟悉,HTTP协议这里就不做讲解了。整个python的代码如下:#!/usr/bin env python import socket host="www.baidu.com" se=socket.socket(socket.AF_INET,socket.SOCK_STREAM) se.connect((ho...

python发送HTTP请求的方法小结

本文实例讲述了python发送HTTP请求的方法。分享给大家供大家参考。具体如下: 这里包含 Python 使用 GET/HEAD/POST 方法进行 HTTP 请求 1. GET 方法:>>> import httplib >>> conn = httplib.HTTPConnection("www.python.org") >>> conn.request("GET", "/index.html") >>> r1 = conn.getresponse() >>> print r1.status, r1.reason 200 OK >>> data1 = r1.read() >>> conn.request("GET", "/parrot.spam") >>> r2 = conn....

python中使用urllib2获取http请求状态码的代码例子

采集内容常需要得到网页返回的验证码做进一步处理 下面代码是用python写的用来获取网页http状态码的脚本#!/usr/bin/python # -*- coding: utf-8 -*- #encoding=utf-8 #Filename:states_code.pyimport urllib2url = http://www.bitsCN.com/ response = None try:response = urllib2.urlopen(url,timeout=5) except urllib2.URLError as e:if hasattr(e, code):print Error code:,e.codeelif hasattr(e, reason):print Reason:,e.rea...

python通过get,post方式发送http请求和接收http响应的方法

本文实例讲述了python通过get,post方式发送http请求和接收http响应的方法。分享给大家供大家参考。具体如下: 测试用CGI,名字为test.py,放在apache的cgi-bin目录下:#!/usr/bin/python import cgi def main(): print "Content-type: text/html\n"form = cgi.FieldStorage()if form.has_key("ServiceCode") and form["ServiceCode"].value != "":print " Hello",form["ServiceCode"].value,"" else: print " Error! Please enter f...

【python】selenium获取http请求头信息【代码】【图】

# 以获取API商城 - IP查询服务的timestamp签名为例# 是seleniumwire 不是 selenium import time from seleniumwire import webdriver driver = webdriver.Chrome()driver.get(https://apis.baidu.com/store/aladdin/land?cardType=ipSearch) driver.find_element_by_xpath(//*[@id="app"]/div[2]/div/div/div[2]/input).send_keys("112.10.36.59") driver.find_element_by_xpath(//*[@id="app"]/div[2]/div/div/div[2]/div).click(...

这可能是最快的python http请求库(类似于requests)

fast_requests 2020年4月29日16:16:03更新: 介绍 用c++写的python库,真多线程多并发,性能超强,多线程下载二进制的文件效果最佳这可能是最快的http请求库,暂支持get方法,线程多的话宽带可以跑满。项目地址:https://github.com/daimiaopeng/fast_requests 测试: 循环get("https://baidu.com") 100 次1.自己写的 3.4 秒2.requests (启用session)10.5 秒3.grequests 31.3 秒循环get("http://www.nbzhuti.cn/") 1000次fast_re...

HTTPS请求HTTP接口被浏览器阻塞,python实现websocket客户端,websocket服务器,跨域问题,dwebsocket,https,拦截,服务端【代码】

HTTPS请求HTTP接口被浏览器阻塞,python实现websocket客户端,websocket服务器,跨域问题,dwebsocket,https,拦截,服务端 发表时间:2020-03-05 1 背景 由于公司前端的页面部署在以https(加了证书)协议的域名下,去请求http协议的域名的某个服务,并且该http域名下的服务,不仅要处理普通请求(POST、GET),还需要处理websocket请求。由于浏览器禁止https域名的内容请求http的服务,甚至嵌入子页面都禁止,因为浏览器会认为http...

python利用多线程让http请求异步返回【代码】

有时我们可能会碰到这样一种情况,我们有一个功能,这个功能对外提供了一个http接口,我们需要对这个http接口发起请求才能启动这个服务,但是这个服务功能可能会执行很久,这样如果等功能执行结束再返回请求结果,那这个请求可能就超时了, ? 发起请求的客户端1 import requests 2 3 req = requests.get("http://127.0.0.1:9898/register?username=aaa&pwd=232323") 4 print(req.content)? 服务端 1 # coding=utf-82 import flas...