简单的Python服务

简单的Python服务

服务端

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from BaseHTTPServer import BaseHTTPRequestHandler
from BaseHTTPServer import HTTPServer

import cgi
import os

import json
import urllib

data = {
    'result': 'this is a test',
    'RSP_HEAD': {'TRAN_SUCCESS': '1'},
    'RSP_BODY': {
        'code': '200',
        'uid': '111000030000010',
        'cno': '1103',
        "ano": "1103",
        "pid": "1130",
        }
    }
host = ('0.0.0.0', 8088)


class Resquest(BaseHTTPRequestHandler):
    def do_GET(self):
        print("to get something......")
        d = self.headers
        # get_data = urllib.parse.parse_qs(self.rfile.read(length).decode('utf-8'))
        print(d)
        self.send_response(200)
        self.send_header('Content-type', 'application/json')
        self.end_headers()
        self.wfile.write(json.dumps(data).encode('utf-8'))

    def do_POST(self):
        print("to parse post......")
        length = int(self.headers['Content-Length'])
        post_data = urllib.parse.parse_qs(self.rfile.read(length).decode('utf-8'))
        print(post_data)
        self.send_response(200)
        self.send_header('Content-type', 'application/json')
        self.end_headers()
        self.wfile.write(json.dumps(data).encode('utf-8'))

if __name__ == '__main__':
    server = HTTPServer(host, Resquest)
    print("Starting server, listen at: %s:%s" % host)
    server.serve_forever()

 

 

客户端 

import requests
import json
import os
 
def test_post():
    conn = requests.session()
    conn.get("http://proxy-auth.demo.cn/sso/cookies")

    url = 'http://test.dev.demo.cn'
    postdata = {
        'uid': '111000030000010',
        'cno': '1103',
        "ano": "1103",
        "pid": "1130",
    }
    headers = {'content-type': "application/json", 'Authorization': 'APP appid = 4abf1a,token = 9480295ab2e2eddb8'}
    rep = conn.post(url, data=postdata, headers=headers)
    print(rep.text)
 
 
if __name__ == "__main__":
    test_post()

 

 

 

 

 

 

你可能感兴趣的:(python)