您的位置:首页 > 理论基础 > 计算机网络

http链接url地址后进行POST、PUT、GET、DEL操作

2016-10-18 06:40 741 查看
现在做的项目,需要经常链接到url地址上查看数据,因此写了个通用的CRUD操作的脚本,不同的操作在method的地方填写。如POST、PUT、DELETE、GET等操作

import httplib
import json
import re
class HttpRequest(object):
def http_request(self, data, requrl, method):
re_str = '.*://(.*)/api'
re_pat = re.compile(re_str)
search_ret = re_pat.search(requrl)
ipaddress_port = search_ret.group()
ipaddress_port_info = ipaddress_port[0]
ipaddress = ipaddress_port_info.split(":")[0]
port = int(ipaddress_port_info.split(":")[1])
post_data = json.dumps(data)
headerdata = {"Content-type":"application/json"}
try:
conn = httplib.HTTPConnection(ipaddress, port)
conn.request(method=method, url=requrl, body=post_data, headers=headerdata)
response = conn.getresponse()
return response.read()
except Exception, e:
print 'error %s' % str(e.args)
finally:
if conn:
conn.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐