http

(图片来自互联网)

如果一个人想学习爬虫技术,我会首先推荐他学会使用httpbin!

httpbin(官网|github)是一个很不错测试工具,你可以放心大胆的他,而不用担心他报复你。他有点像一个蜜罐,时刻等待着你的光临,然后根据你的请求,给你返回你想要的东西

相同推荐

类似这种能够很方便调试接口的还有很多,但是无疑还是httpbin最好用。

requestb

putsreq

ttpresponder

runscope

如何使用

使用你擅长的语言,与它的接口互动。

接口

接口列表

EndpointDescription
/This page.
/ipReturns Origin IP.
/user-agentReturns user-agent.
/headersReturns header dict.
/getReturns GET data.
/postReturns POST data.
/patchReturns PATCH data.
/putReturns PUT data.
/deleteReturns DELETE data
/gzipReturns gzip-encoded data.
/deflateReturns deflate-encoded data.
/status/:codeReturns given HTTP Status code.
/response-headersReturns given response headers.
/redirect/:n302 Redirects n times.
/redirect-to?url=foo302 Redirects to the foo URL.
/relative-redirect/:n302 Relative redirects n times.
/cookiesReturns cookie data.
/cookies/set?name=valueSets one or more simple cookies.
/cookies/delete?nameDeletes one or more simple cookies.
/basic-auth/:user/:passwdChallenges HTTPBasic Auth.
/hidden-basic-auth/:user/:passwd404'd BasicAuth.
/digest-auth/:qop/:user/:passwdChallenges HTTP Digest Auth.
/stream/:nStreams n – 100 lines.
/delay/:nDelays responding for n – 10 seconds.
/dripDrips data over a duration after an optional initial delay, then (optionally) returns with the given status code.
/range/:nStreams n bytes, and allows specifying a Range header to select a subset of the data. Accepts a chunk_size and request duration parameter.
/htmlRenders an HTML Page.
/robots.txtReturns some robots.txt rules.
/denyDenied by robots.txt file.
/cacheReturns 200 unless an If-Modified-Since or If-None-Match header is provided, when it returns a 304.
/cache/:nSets a Cache-Control header for n seconds.
/bytes/:nGenerates n random bytes of binary data, accepts optional seed integer parameter.
/stream-bytes/:nStreams n random bytes of binary data, accepts optional seed and chunk_size integer parameters.
/links/:nReturns page containing n HTML links.
/forms/postHTML form that submits to /post
/xmlReturns some XML
/encoding/utf8Returns page containing UTF-8 data.

简单例子

import requests
s = requests.Session()

print s.get('http://httpbin.org/ip').text

print s.get('http://httpbin.org/get').json()

print s.post('http://httpbin.org/post', {'key':'value'},headers={'user-agent':'LAOGAO'}).text

print s.get('http://httpbin.org/status/404').status_code

print s.get('http://httpbin.org/html').text

print s.get('http://httpbin.org/deny').text

技巧

httpbin给我们提供了很多HTTP基本的操作,如果你的APP组问你要接口,你手头又很忙,怎么办?当然是用httpbin按照接口文档先给他们快速写几个丢过去。

部署

httpbin使用flask编写,可以参考使用nginx + uWSGI运行flask程序,不过在此老高又学会了一种新的方法 -- 使用gunicorn运行flask。

gunicorn又是一个 Python WSGI HTTP Server for UNIX!

# 如果已经安装PIP,请忽略此步骤
cd ~ && wget -O - "https://bootstrap.pypa.io/get-pip.py" | python

# 安装httpbin
pip install httpbin

# 安装服务器软件gunicorn
pip install gunicorn

# 运行httpbin,在8000端口
gunicorn -b :8000 httpbin:app

# [2015-06-27 03:16:52 +0000] [18568] [INFO] Starting gunicorn 19.3.0
# [2015-06-27 03:16:52 +0000] [18568] [INFO] Listening at: http://0.0.0.0:8000 (18568)
# [2015-06-27 03:16:52 +0000] [18568] [INFO] Using worker: sync
# [2015-06-27 03:16:52 +0000] [18573] [INFO] Booting worker with pid: 18573

接下来访问你的IP:8000,不出意外,你就能看到与官网一样的功能了!

httpbin on localhost

使用文档

gunicorn文档

pip文档

使用PHPSTORM RESTful调试

Tools | Test RESTful 中可以找到Test RESTful工具,可以方便的调试接口。

RESTful

标签: http, python, pip, httpbin, gunicorn, restful

已有 4 条评论

  1. [...]后端将示例接口返回数据写在文件里,接口直接返回静态文件数据。此种方式,由后端定义接口数据格式。另外,有一个开源的工具: httpbin可以用来提供接口返回指定格式的数据,中文介绍可见:https://blog.phpgao.com/how-to-httpbin.html。[...]

  2. 如果一个人像(想吧)学习爬虫

添加新评论