前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python杂记

Python杂记

作者头像
py3study
发布2020-01-14 15:38:26
4580
发布2020-01-14 15:38:26
举报
文章被收录于专栏:python3python3

Python 2.7

  1. 先装easy install https://pypi.python.org/pypi/setuptools#downloads 可以下载,在win7 cmd下用python ez_setup.py安装
  2. 也可以装个IPython shell http://archive.ipython.org/release/
  3. 后自动补全的Pyreadline https://pypi.python.org/pypi/pyreadline
  4.  提供http post支持的multipartposthandler https://pypi.python.org/packages/2.7/M/MultipartPostHandler2/ 在windows cmd下 >easy_install MultipartPostHandler2-0.1.5-py2.7.egg
  5. pip 安装和管理 Python 包的工具 , 是 easy_install 的一个替换品 https://pip.pypa.io/en/latest/installing.html >python get-pip.py
  6. 新的包格式wheel https://pypi.python.org/pypi/wheel >pip install --user wheel
  7.  http request https://pypi.python.org/pypi/requests/  >pip install requests

一些例子:

代码语言:javascript
复制
#!/usr/bin/python2.7
import sys
import os
import json
import MultipartPostHandler 
import urllib2
import requests
import time
import telnetlib
bridgeip = '192.168.0.166'
targetlamp = []
base_url = 'http://'+bridgeip+'/api/'+username
config_url = base_url + '/config'
path = os.getcwd()
print('filepath = ' + path)
#使用MultipartPostHandler
def postBridge():
    log('Posting reset firmware file to the bridge')
    filepath = os.path.join(path, 'FirmwareBridge', 'reset', 'rel.fw') #looks in the directory ../<script execution dir>/FirmwareBridge/reset/ for rel.fw
    opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
    params = {'fileupload' : open(filepath, 'rb')}
    opener.open('http://'+bridgeip+'/updater', params)
    log( 'Bridge is restarting ...')
    time.sleep(60)
#使用requests
def setBridgeVersion(bridgeVersion):
    log('Posting firmware file to the bridge')
    filepath = os.path.join(path, 'FirmwareBridge', bridgeVersion, 'rel.fw')
    #filepath = os.path.join(path, 'FirmwareBridge', bridgeVersion, 'prod.fw')
    firmwareBridge = {'firmwarefile' : open(filepath, 'rb')}
    fileuploadurl = 'http://'+bridgeip+'/updater'
    response = requests.post(fileuploadurl, files=firmwareBridge)
    print response.text
    time.sleep(60)
#httpget,json操作
def fillLighsArray():
    r = requests.get(base_url)
    config = r.text
    configjson = json.loads(config)
    lights = json.dumps(configjson['lights'])
    lightsjson = json.loads(lights)
    for i in range(1,len(lightsjson)+1):
        targetlamp.append(i)
#http put

def setPortalServices(set_value):
    body = '{"portalservices":' + set_value + '}'           
    r = requests.put(config_url,body)
    log(  "Setting portal services to [" + set_value + "] : " + r.text)
检查ping结果
def pingHost(host):
 p = subprocess.Popen("ping -n 1 "+ host,
 stdin = subprocess.PIPE,
 stdout = subprocess.PIPE,
 stderr = subprocess.PIPE,
 shell = True,
 bufsize = 0)
 (stdoutput,erroutput) = p.communicate() 
 regex = re.compile("Minimum = (\d+)ms, Maximum = (\d+)ms, Average = (\d+)ms", re.IGNORECASE)
 print("find %d\n",len(regex.findall(stdoutput)))
 if len(regex.findall(stdoutput)) > 0:
 #print host+': Host Up!'
 return True
 else:
 #print host+': Host Down!'
 return False
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档