美文网首页
python-http get post

python-http get post

作者: 巴巴11 | 来源:发表于2020-04-14 23:27 被阅读0次

python -v
3.7.2

httpget.py

#!/usr/bin/env python3
# _*_ coding="utf-8" _*_

#######
# author : xy
# data : 2001/01/01
# despcription : desc
######


import http.client

server = "127.0.0.1"
port = 8080
_timeout = 10

client = http.client.HTTPConnection(server, port, timeout = _timeout)

if __name__ == "__main__":
    client.request('GET', '/rest/test/t1')
    res = client.getresponse()
    print(res.status)
    print(res.read())

httppost.py

#!/usr/bin/env python3
# _*_ coding="utf-8" _*_

#######
# author : xy
# data : 2001/01/01
# despcription : desc
# 参考: https://www.cnblogs.com/mingerlcm/p/11369444.html
# https://blog.csdn.net/joye123/article/details/93983121
######


import http.client
import json

server = "127.0.0.1"
port = 8080
_timeout = 10

client = http.client.HTTPConnection(server, port, timeout = _timeout)


p = {
    'kw' : '苹果',
}

headers = {'Accept-Charset': 'utf-8', 'Content-Type': 'application/json'}
params = json.dumps(p)
params = bytes(params, 'utf8')

if __name__ == "__main__":
    client.request('POST', '/rest/test/t2', headers = headers, data=params)
    res = client.getresponse()
    print(res.status)
    print(res.read())

相关文章

网友评论

      本文标题:python-http get post

      本文链接:https://www.haomeiwen.com/subject/sjyfvhtx.html