コード例 #1
0
		real_url = "https://www.googleapis.com/language/translate/v2?key=%s&source=zh-CN&target=%s&q=%s" % (GOOGLE_API_KEY, lang, text);

		if debug:
			print "[request] " + real_url

		result = urllib2.urlopen(real_url).read()

		# cache it!
		cache_file = open(targetFile, 'w')
		cache_file.write(result)
		cache_file.close()
	else: # hit
		if debug:
			print "[hit] " + text
		cache_file = open(targetFile, 'r')
		result = cache_file.read()
		cache_file.close()

	if debug:
		print result

	server.bottle.response.set_header('Access-Control-Allow-Origin', '*')

	jsonp_result = "trans_result(" + result + ")"

	return jsonp_result

server.bottle.debug(debug)
run(host='116.255.180.236', port='9999')
コード例 #2
0
		real_url = "https://www.googleapis.com/language/translate/v2?key=%s&source=zh-CN&target=%s&q=%s" % (GOOGLE_API_KEY, lang, text);

		if debug:
			print "[request] " + real_url

		result = urllib2.urlopen(real_url).read()

		# cache it!
		cache_file = open(targetFile, 'w')
		cache_file.write(result)
		cache_file.close()
	else: # hit
		if debug:
			print "[hit] " + text
		cache_file = open(targetFile, 'r')
		result = cache_file.read()
		cache_file.close()

	if debug:
		print result

	server.bottle.response.set_header('Access-Control-Allow-Origin', '*')

	jsonp_result = "trans_result(" + result + ")"

	return jsonp_result

server.bottle.debug(debug)
run(host='0.0.0.0', port='9999')