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使用正则表达式分割字符串的实现方法

如下: re.split(pattern, string, [maxsplit], [flags]) pattern:表示模式字符串,由要匹配的正则表达式转换而来。 string...

Python3实现的旋转矩阵图像算法示例

本文实例讲述了Python3实现的旋转矩阵图像算法。分享给大家供大家参考,具体如下: 问题: 给定一个 n × n 的二维矩阵表示一个图像。 将图像顺时针旋转 90 度。 方案一:先...

python获取代码运行时间的实例代码

有的时候,操作大文件,或者取数,要很久,我们给脚本首尾添加一段代码就知道,这段代码整体的大致运行时间了。 import time start =time.clock() #中间写上代...

python之验证码生成(gvcode与captcha)

python之验证码生成(gvcode与captcha)

今天向大家总结一下python在做项目时用到的验证码生成工具:gvcode与captcha gvcode 全称:graphic-verification-code 安装: pip i...

Java中重定向输出流实现用文件记录程序日志

System中的out,error都是final类型的,不能做改动。但通过setOut()可以设置新的输出流,从而实现写日志的功能。 import java.io.PrintStre...