第一个图灵机器人接口实例:

#!/usr/bin/env python
# -*- coding: utf_8 -*-
import urllib2,urllib
import json
import unittest, time, re class APITest():
'''
接口测试类
'''
def apicall(self,method,url,getparams,postparams):
str1=''
#GET方法调用
if method=='GET':
if getparams!="":
for k in getparams:
str1=str1+k+'='+urllib2.quote(str(getparams.get(k)))
if len(getparams)>2:
str1=str1+"&"
url=url+"&"+str1;
result = urllib2.urlopen(url).read()
#POST方法调用
if method=='POST':
# if postparams!="":
data = urllib.urlencode(postparams)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
result = response.read()
jsdata=json.loads(result)
return jsdata class APIGetAdList(unittest.TestCase):
def test_call(self):
api=APITest()
getparams=''
KEY = '***************************'
req_info = u'讲个笑话'.encode('utf-8')
postparams={'key': KEY, 'info': req_info}
data=api.apicall('POST','http://www.tuling123.com/openapi/api',getparams,postparams).get('text').replace('<br>', '\n')
print data if __name__ == "__main__":
unittest.main()

第二个图灵机器人接口实例:

# -*- coding: utf-8 -*-
import json
import requests
import urllib
import urllib2 KEY = '*********************' # change to your API KEY
url = 'http://www.tuling123.com/openapi/api' req_info = u'讲个笑话'.encode('utf-8') query = {'key': KEY, 'info': req_info}
headers = {'Content-type': 'text/html', 'charset': 'utf-8'} # 方法一、用requests模块已get方式获取内容
r = requests.get(url, params=query, headers=headers)
res = r.text
print json.loads(res).get('text').replace('<br>', '\n') # 方法二、用urllib和urllib2库获取内容
data = urllib.urlencode(query)
req = urllib2.Request(url, data)
f = urllib2.urlopen(req).read()
print json.loads(f).get('text').replace('<br>', '\n')

最新文章

  1. 查看Sql Server被锁的表以及解锁
  2. WebAPI中无法获取Session对象的解决办法
  3. SQLite剖析之功能特性
  4. SQL Server 2012不支持从SQL Server 2000的备份进行还原
  5. 打开VS2012的创建单元测试功能
  6. Java-链表LinkedList源码原理分析,并且通过LinkedList构建队列
  7. WPF开发时光之痕日记本
  8. 控制UIlabel 垂直方向对齐方式的 方法
  9. AngularJS - contorller app module
  10. 当linux遇上多网卡时
  11. 【C++11】新特性——Lambda函数
  12. selenium之多线程启动grid分布式测试框架封装(三)
  13. SSMS2008插件开发(2)--Microsoft Visual Studio 2008插件开发介绍
  14. Spring Mybatis-分页插件使用
  15. php引用传值详解
  16. eclipse快速配置spring相关xml文件头信息
  17. JSP标准标签库:JSTL
  18. 【ES】学习2-搜索
  19. linux文件系统扩展属性
  20. Eclipse如何导入DemoWeb.rar

热门文章

  1. HDU 5794:A Simple Chess(Lucas + DP)
  2. PHP开发异步高性能的MySQL代理服务器
  3. Postgres-XL介绍
  4. sass初步认识3
  5. 每日一九度之 题目1031:xxx定律
  6. 【转】SVN建库方法
  7. 理解 Android Build 系统
  8. android -- 蓝牙 bluetooth (五)接电话与听音乐
  9. CallableAndFuture
  10. hiho 第119周 最大权闭合子图