发布时间:2019-09-21 10:49:27编辑:auto阅读(1618)
闲言少述,直接上代码.
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
上一篇: Python知识结构图
下一篇: 【Python】查看Python已安装的
47839
46383
37274
34725
29311
25968
24899
19946
19538
18019
5788°
6410°
5925°
5959°
7062°
5908°
5941°
6435°
6402°
7774°