Python 官方文档:入门教程 => 点击学习
、JSON简介 jsON(javascript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和
闲言少述,直接上代码.
1. GET方法
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# File: http_get.py
import urllib2
def http_get():
url='http://192.168.1.13:9999/test' #页面的地址
response = urllib2.urlopen(url) #调用urllib2向服务器发送get请求
return response.read() #获取服务器返回的页面信息
ret = http_get()
print("RET %r" % (ret))
2. POST方法
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# File http_post.py
import urllib
import urllib2
import json
def http_post():
url='http://192.168.1.13:9999/test'
values ={'user':'Smith','passwd':'123456}
headers = {'Content-Type': 'application/json'} #这一行最好加上
req = urllib2.Request(url = url, headers=headers, data = json.dumps(values) ) # 生成页面请求的完整数据
response = urllib2.urlopen(req) # 发送页面请求
return response.read() # 获取服务器返回的页面信息
resp = http_post()
print resp
3. PUT方法
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# File: http_put.py
import urllib2
import json
def http_put():
url='http://192.168.1.13:9999/test'
values={'':''}
jdata = json.dumps(values) # 对数据进行JSON格式化编码
request = urllib2.Request(url, jdata)
request.add_header('Content-Type', 'your/conntenttype')
request.get_method = lambda:'PUT' # 设置HTTP的访问方式
request = urllib2.urlopen(request)
return request.read()
resp = http_put()
print resp
4. DELETE方法
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# File: http_delete.py
import urllib2
import json
def http_delete():
url='http://192.168.1.13:9999/test'
values={'user':'Smith'}
jdata = json.dumps(values)
request = urllib2.Request(url, jdata)
request.add_header('Content-Type', 'your/conntenttype')
request.get_method = lambda:'DELETE' # 设置HTTP的访问方式
request = urllib2.urlopen(request)
return request.read()
resp = http_delete()
print resp
--结束END--
本文标题: python用 GET,POST,PUT
本文链接: https://lsjlt.com/news/191217.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0