python client使用http post 到server端的代码

yipeiwu_com5年前Python基础
复制代码 代码如下:

import urllib, httplib 
import utils 
import json
       class User: 

        def __init__(self): 
            print 'a' 

        def login(self, imsi, ua): 
            print "==============user start login==================" 
            input = { 
                "method"       : "user.login", 
                "userName"     : "", 
                "userPass"     : "", 
            } 

            input["sig"] = utils.getSignature(input) 
            params = urllib.urlencode(input) 
            headers = { 
                "user-agent"  : ua, 
                "Appstore-clientType" : "android", 
                "Appstore-IMEI" : "123456789000000", 
                "Appstore-IMSI" : imsi 
            } 

            try: 
                connection = httplib.HTTPConnection(utils.API_HOST) 
                connection.request("POST", "/api", params, headers) 
                response = connection.getresponse().read() 
                #print "=========" + response 
                connection.close() 
            except Exception, e : 
                print "========" + str(e)     

            if "errorcode" in response or response is None: 
                return 

            results = json.loads(response)     

            return results["results"].encode("utf-8")

相关文章

python实现年会抽奖程序

用python来实现一个抽奖程序,供大家参考,具体内容如下 主要功能有 1.从一个csv文件中读入所有员工工号 2.将这些工号初始到一个列表中 3.用random模块下的choice...

Python3 requests文件下载 期间显示文件信息和下载进度代码实例

这篇文章主要介绍了Python3 requests文件下载 期间显示文件信息和下载进度代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以...

python 图片二值化处理(处理后为纯黑白的图片)

python 图片二值化处理(处理后为纯黑白的图片)

先随便招一张图片test.jpg做案例 然后对图片进行处理 # 图片二值化 from PIL import Image img = Image.open('test.jpg')...

Python编程中NotImplementedError的使用方法

Python编程中raise可以实现报出错误的功能,而报错的条件可以由程序员自己去定制。在面向对象编程中,可以先预留一个方法接口不实现,在其子类中实现。 如果要求其子类一定要实现,不实现...

在Python编程过程中用单元测试法调试代码的介绍

对于程序开发新手来说,一个最常见的困惑是测试的主题。他们隐约觉得“单元测试”是很好的,而且他们也应该做单元测试。但他们却不懂这个词的真正含义。如果这听起来像是在说你,不要怕!在这篇文章中...