配置好http服务器后,需要对http服务器进行简单的get和post测试,下面是利用twisted的实现的一个简易web 客户端
#encoding: utf-8
'''
web客户端,模拟请求http服务器的处理过程
'''
import sys
import base64
from twisted.internet import reactor
from twisted.web.client import getPage
gGMConf = [
"GM发起指令: python web_client.py start host uid",
"GM发送数据: python web_client.py send chat_id msg",
"GM接收数据: python web_client.py receive chat_id" ,
"GM关闭会话: python web_client.py end chat_id",
]
gGSConf = [
]
def printPage(result):
print result
def printError(fail):
print fail
def stop(result):
reactor.stop()
def handle(url, **args):
if args:
d = getPage(url, method = args["method"], postdata = args["msg"])
else:
d = getPage(url)
d.addCallbacks(printPage, printError)
d.addBoth(stop)
if __name__ == "__main__":
args = sys.argv
if len(args) < 2:
print "err", args
exit(1)
wtype = args[1]
url = URL % wtype
if wtype == "?":
for msg in gGMConf:
print msg
exit(0)
elif wtype == "start":
url += "&host=%s&uid=%s" % ( args[2], args[3] )
handle(url)
elif wtype in ( "send", "receive", "end" ):
url += "&chat_id=%s" % args[2]
if wtype == "send":
handle(url, method = "POST", msg = base64.b64encode(args[3]))
else:
handle(url)
reactor.run()
由于隐私要求,代码中的具体URL已经略去,核心处理部分为get_page,既可以发送get请求,也可以发送post请求。
URL大致的格式为:http://ip地址/uwsgi/ticket_gm?action=%s