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

yipeiwu_com6年前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")

相关文章

node.js获取参数的常用方法(总结)

1、req.body 2、req.query 3、req.params 一、req.body例子 body不是nodejs默认提供的,你需要载入body-parser中间件才可以使用re...

Django中ajax发送post请求 报403错误CSRF验证失败解决方案

Django中ajax发送post请求 报403错误CSRF验证失败解决方案

前言 今天学习Django框架,用ajax向后台发送post请求,直接报了403错误,说CSRF验证失败;先前用模板的话都是在里面加一个 {% csrf_token %} 就直接搞定了C...

Windows 安装 Anaconda3+PyCharm的方法步骤

Windows 安装 Anaconda3+PyCharm的方法步骤

由于本人使用的是windows 10 操作系统,所以介绍在 windows 10 系统中安装 Anaconda3 的过程。 下载 Anaconda 官网下载地址:https://www....

Python实现密码薄文件读写操作

制作一个"密码薄",其可以存储一个网址,和一个密码(如 123456),请编写程序完成这个“密码薄”的增删改查功能,并且实现文件存储功能 D:\pytest_day\mimab...

python的dataframe和matrix的互换方法

实例如下所示: #-*- encoding:utf-8 -*- import pandas as pd import numpy as np df = pd.DataFrame(np...