python(http\json\file)

本文通过实例介绍了使用Python进行网络请求的基本方法,包括GET和POST请求的实现,以及JSON数据的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python 简明教程

 

http:

---------------------get-------------------------------

01 >>> import httplib   
02 >>> conn = httplib.HTTPConnection("www.python.org")   
03 >>> conn.request("GET", "/index.html")   
04 >>> r1 = conn.getresponse()   
05 >>> print r1.status, r1.reason,r1.read()  
06 200 OK   
07 >>> data1 = r1.read()   
08 >>> conn.request("GET", "/parrot.spam")   
09 >>> r2 = conn.getresponse()   
10 >>> print r2.status, r2.reason   
11 404 Not Found   
12 >>> data2 = r2.read()   
13 >>> conn.close()

 

--------------------post----------------------------------

01 >>> import httplib, urllib   
02 >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})   
03 >>> headers = {"Content-type": "application/x-www-form-urlencoded",   
04 ...            "Accept": "text/plain"}   
05 >>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80")   
06 >>> conn.request("POST", "/cgi-bin/query", params, headers)   
07 >>> response = conn.getresponse()   
08 >>> print response.status, response.reason   
09 200 OK   
10 >>> data = response.read()   
11 >>> conn.close()

 

 

--------------------post---------------------------------- 

  7 conn = httplib.HTTPConnection("noah.baidu.com")
  8 conn.request("GET", "/webfoot/index.php?r=webfoot/GetServiceInfo&serviceName=dsb.OPDB.ai")
  9 r1 = conn.getresponse()
 10 print r1.status, r1.reason
 11 print r1.read()
 12 if conn:
 13     conn.close()
 14
 15 params = urllib.urlencode({'r': 'webfoot/EnableServiceInstances', 'serviceName': 'dsb.OPDB.ai',
 16         'authKey': '43c5d69ee07a35f6146bf61157771f14', 'enable': 'disable', 'machines': 'yf-dba-csp-dsb00.yf01'})
 17 headers = {"Content-type": "application/x-www-form-urlencoded"}
 18 conn = httplib.HTTPConnection("noah.baidu.com", timeout=30)
 19 conn.request("POST", "/webfoot/index.php", params, headers)
 20 response = conn.getresponse()
 21 print response.status, response.reason
 22 print response.read()
 23 if conn:
 24     conn.close()

 

--------------------json---------------------------------- 

import httplib
import json
 32 def GetInstanceInfo(ServiceName):
 33     command = "/webfoot/index.php?r=webfoot/GetInstanceInfo&serviceName=%s" % ServiceName
 34     conn = httplib.HTTPConnection("noah.baidu.com")
 35     conn.request("GET", command)
 36     r1 = conn.getresponse()
 37     #print r1.status, r1.reason
 38     print "================InstanceInfo==================="
 39     if 200 == r1.status:
 40         result = "%s" % r1.read()
 41         #print result
 42         hjson = json.loads(result)
 43         if 0 == hjson['retCode']:
 44             for i in range(len(hjson['instanceInfo'])):
 45                 print "hostName  :", hjson['instanceInfo'][i]['hostName']
 46                 print "port      :", hjson['instanceInfo'][i]['port']
 47                 print "status    :", hjson['instanceInfo'][i]['status']
 48                 print "enable    :", hjson['instanceInfo'][i]['interventionalStatusMean']
 49                 print "tag       :", hjson['instanceInfo'][i]['tag']
 50                 print "deployPath:", hjson['instanceInfo'][i]['deployPath']
 51         else:
 52             print result
 53     else:
 54         print r1.status, r1.reason
 55     if conn:
 56         conn.close()

 

==============================file=============================================

 13 import ConfigParser

 14 def xdb_viewfreebgw(args):
 15     cf = ConfigParser.ConfigParser()
 16     cf.read("bgw.conf")
 17     bgqs = []
 18     for i in range(len(cf.sections())):
 19         sname = "vip_{0}".format(i)
 20         idc = cf.get(sname, 'idc')
 21         ip = cf.get(sname, 'ip')
 22         port = cf.getint(sname, 'port')
 23         status = cf.getint(sname, 'status')
 24         appname = cf.get(sname, 'appname')
 25         bminame = cf.get(sname, 'bminame')
 26         bgq = BGQ(idc, ip, port, status, appname, bminame, sname, cf)
 27         bgqs.append(bgq)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值