Python Http 请求

如果要进行客户端和服务器端之间的消息传递,我们可以使用HTTP协议请求

HTTP 协议请求主要分6种类型 (GET 和 POST 较常用)

1)GET 请求

通过URL网址传递信息,可以直接在URL中写上要传递的信息,也可以由表单进行传递(表单中的信息会自动转化为URL地址中的数据,通过URL地址传递)

备注:已经取得资源,并将资源添加到响应中的消息体

2)POST 请求

可以向服务器提交数据,是一种比较安全的数据传递方式,比如在登录时,经常使用 POST 请求发送数据

3)PUT 请求

请求服务器存储一个资源,通常需要制定存储的位置

4)DELETE 请求

请求服务器删除一个资源

5)HEAD 请求

请求获取对应的 HTTP 报头信息

6)OPTIONS 请求

可以获得当前URL所支持的请求类型

Response Code

状态码:200 OK
表明请求已经成功. 默认情况下成功的请求将会被缓存

不同请求方式对于请求成功的意义如下:
GET:已经取得资源,并将资源添加到响应中的消息体.
HEAD:作为消息体的头部信息
POST:在消息体中描述此次请求的结果

请求成功对于PUT 和 DELETE 来说并不是200 ok 而是 204 所代表的没有资源 (或者 201 所代表的当一个资源首次被创建成功

以下是常见状态码及含义

状态码 英文 中文含义
200 OK 一切正常
301 Moved Permanently 重定向到新的URL,永久性
302 Found 重定向到临时URL,非永久性
304 Not Modified 请求的资源未更新
400 Bad Request 非法请求
401 Unauthorized 请求未经授权
403 Forbidden 禁止访问
404 Not Found 没有找到对应资源
500 Internal Server Error 服务器内部出现错误
501 Not Implemented 服务器不支持实现请求所需要的功能
import urllib.request
import urllib.parse
import requests

resp = urllib.request.urlopen("http://www.baidu.com/")
html = resp.read().decode("utf-8")
print(html)

# GET
resp = urllib.request.urlopen("http://localhost:8080/sayHello?name=zhangsan")
print(resp.status)
print(resp.read().decode("UTF-8"))

# POST
data = urllib.parse.urlencode({"username": "lisi", "password":"123456"})
data = data.encode("UTF-8")
resp = urllib.request.urlopen("http://localhost:8080/login", data)
print(resp.read().decode("UTF-8"))

# 带Header
header = {
    "POSID": "1010101"
}
req = urllib.request.Request("http://localhost:8080/login", data, header)
resp = urllib.request.urlopen(req)
print(resp.read().decode("UTF-8"))

# requests
# GET
response = requests.get("http://localhost:8080/sayHello", "name=zhangsan")
print(response.status_code)
print(response.headers)
print(response.headers["Content-Type"])
print(response.encoding)
print(response.text)

# POST
# 类似jQuery
# response = requests.post("http://localhost:8080/login", {"username":"lisi","password":"123456"})
response = requests.post("http://localhost:8080/login", "username=lisi&password=123456")
if response.status_code == 200:
    print(response.text)
    print(response.json())

# 带headers
url = "http://localhost:8080/login"
data = "username=lisi&password=123456"
headers = {"id":"123", "name": "abc"}
response = requests.post(url, data, headers=headers, timeout=3)
if response.status_code == 200:
    print(response.text)
    print(response.json())

#传json数据
 import  urllib2
  

jsonstr='{
    "rd": {
        "a": "a",
        "b": "b",
        "c": "c",
        "d": "d",
        "e": "d",
        "f": "f",
        "g": "g",
     
    },
    "rds": [{
        "rdsa": "rdsa",
        "rdsb": "rdsb",
        "rdsc": rdsc,
        "rdsd": 1.0,
        "rdse": 1.3,
     
     
    }, {
        "rdsa": "rdsa2",
        "rdsb": "rdsb2",
        "rdsc": rdsc2,
        "rdsd": 1.0,
        "rdse": 1.3,
     
    }]
}'

 Post_url = "http:/localhost/api/test"
Post_data = jsonstr
headers = {'Content-Type': 'application/json'}
 request = urllib2.Request(Post_url, Post_data, headers)
  response = urllib2.urlopen(request)
 message = response.read()
r = json.loads(message)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容