interface

#-*-coding:utf-8-*-


import urllib,urllib2
import xlrd
class Interface_test:
    def get_interface(self,menthod,url,data):
       # data=urllib.urlencode(data)
        count=len(data)
        print count
        if menthod=='GET':
            if count==0:
                f=urllib.urlopen(url)
                result=f.read()
                return result
            else:
                data=urllib.urlencode(data)
                url=url+'?'+ data
                print url
                response=urllib.urlopen(url)
                result=response.read()
                return result
        if menthod=='POST':
            data=urllib.urlencode(data)
            req=urllib2.Request(url,data)
            response=urllib2.urlopen(req)
            result=response.read()
            return result
    def get_data(self):
        data={}
        bk=xlrd.open_workbook("D:\Users\ex-pengfei913\Desktop\sms.xlsx")
        sh=bk.sheet_by_name(u'Sheet1')


        nrows=sh.nrows
        ncols=sh.ncols
        for i in range(1,nrows,):
            testcase=sh.cell_value(i,0)
            menthod=str(sh.cell_value(i,1))
            url=str(sh.cell_value(i,2))
            for j in range(3,ncols,2):
                if sh.cell_value(i,j)==" ":
                    key_=" "
                    value_=" "
                else:
                    key_=str(sh.cell_value(i,j))
                    value_=int(sh.cell_value(i,j+1))
                    data[key_]=value_
            data[key_]=value_
        return data,url,menthod
if __name__=="__main()__":
     print("This program is being run by itself")
     Interface_test().get_data()
else:

     print("I am being imported from another module")


一:官网下载python版本(2.6.9)

2.Tar-zvxf python-2..6.9

3.建立pip:

4.Wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb"

5.建立setup:

wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-12.0.3.tar.gz\#md5\=f07e4b0f4c1c9368fcd980d888b29a65

6.建立vartualenv:sudo pipinstall virtualenvwrapper

7.查看virtualenv:pipfreeze|grep virtualenv

8.virtualenv .env

9.source .env/bin/activate

10. freeze

11. install requests

 

二:启动虚拟python环境

1.      source .env/bin/activate

2.      pip freeze(检查.env下面的插件)

3.      gunicorn httpin:app(启动程序)

gunicorn下载:pip install gunicorn/pip installgit+https://github.com/benoitc/gunicorn.git

4.      pip install httpbin(包含插件decorator,click, MarkupSafe, Jinja2, Werkzeug, itsdangerous, Flask, six, httpbin)

5.      #urllib和urllib2是相互独立的模块

6.      #requests库使用的是urllib3(多次请求重复使用一个socket)

三:使用urllib与urllib2写的脚本

import urllib

import urllib2

 

URL_IP = 'http://httpbin.org/ip'

URL_GET = 'http://httpbin.org/get'

 

def use_simple_urllib2():

  response =urllib2.urlopen(URL_IP)

  print'>>>>Response Headers:'

  printresponse.info()

  print'>>>>Response Body:'

  print ''.join([linefor line in response.readlines()])

 

def use_params_urllib2():

#构建请求参数

  params =urllib.urlencode({'params1':'hello','params2':'world'})

  print 'RequestParams:'

  print params

#发送请求

  response =urllib2.urlopen('?'.join([URL_GET, '%s']) % params)

#处理响应

  print'>>>>Response Headers:'

  printresponse.info()

  print'>>>> Status Code:'

  printresponse.getcode()

  print'>>>>Response Body:'

  print ''.join([linefor line in response.readlines()])

 

if __name__ =='__main__':

  print'>>>Use_simple_urllib2:'

 use_simple_urllib2()

  print

  print'>>>Use_params_urllib2:'

 use_params_urllib2()

  print

 

 

 

 

 

四:使用requests请求

# -*- coding:utf-8 -*-

import requests

 

URL_IP = 'http://httpbin.org/ip'

URL_GET = 'http://httpbin.org/get'

 

def use_simple_requests():

 response = requests.get(URL_IP)

 print '>>>>Response Headers:'

 print response.headers

 print '>>>>Response Body:'

 print response.text

 

def use_params_requests():

#构建请求参数

 params = {'params1':'hello','params2':'world'}

#发送请求

 response = requests.get(URL_IP,params=params)

#处理响应

 print '>>>>Response Headers:'

  printresponse.headers

 print '>>>> Status Code:'

 print response.status_code

 print '>>>>Response Body:'

 print response.json()

 

if __name__ =='__main__':

 print '>>>use_simple_requests:'

 use_simple_requests()

 print

 print '>>>use_params_requests:'

 use_params_requests()

 







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值