unity支持python吗_Unity的http通信--unity与python的django通信

http://blog.csdn.net/chenggong2dm/article/details/17372203

写在前面:

WWW类,是unity里,简单的访问网页的类。本文介绍的就是这种方式,与web服务器之间进行通信。当然,HTTP通信,也可以自己通过socket去写,自己实现一个http通信。

WWW类可以用来发送GET和POST请求到服务器,WWW类默认使用GET方法,并且如果提供一个postData参数可用POST方法。这里我们主要使用实用性更强一些的POST方式。

WWW的完整构造函数如下:

WWW( url:string, postData:byte[],headers:Hashtable )

url

The url to download.

postData

A byte array of data to be posted to the url.

headers

A hash table of custom headers to send with the request.

■注意:这个构造函数,有函数重载,可以省略第三个headers参数,也就是:

WWW( url:string, postData:byte[] )

实际例子:

1,新建一个空项目。【File】-->【New Project】

2,新建一个2D背景,用于衬托UI。【GameObject】-->【CreateOther】-->【GUI Texture】

3,写HttpTest.cs脚本文件,绑定到摄像机上。代码如下:

usingUnityEngine;

usingSystem.Collections;

publicclassHttpTest : MonoBehaviour {

//variables

publicstringstr_uid ="";

publicstringstr_score ="";

publicstringstr_response ="";

// Use this for initialization

voidStart () {

}

// Update is called once per frame

voidUpdate () {

}

//在C#中, 需要用到yield的话, 必须建立在IEnumerator类中执行

IEnumerator TestPost()

{

//WWW的三个参数: url, postData, headers

stringurl ="http://127.0.0.1/test/";

byte[] post_data;

Hashtable headers; //System.Collections.Hashtable

stringstr_params;

str_params = "uid="+ str_uid +"&"+"score="+ str_score;

post_data = System.Text.UTF8Encoding.UTF8.GetBytes(str_params);

//Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

//byte[] post_data = encode.GetBytes("uid=中文&score=100");

headers = newHashtable();

//headers.Add("Content-Type","application/x-www-form-urlencoded");

headers.Add("CONTENT_TYPE","text/plain");

//发送请求

WWW www_instance = newWWW(url, post_data, headers);

//web服务器返回

yield returnwww_instance;

if(www_instance.error !=null)

{

Debug.Log(www_instance.error);

}

else

{

this.str_response = www_instance.text;

}

}

voidOnGUI () {

GUI.Label(newRect(10,20,60,20),"UID: ");

GUI.Label(newRect(10,45,60,20),"Score: ");

//注意:因为每一帧都在刷, 所以[文本框]是这种写法:

str_uid = GUI.TextField(newRect(60, 20, 160, 20), str_uid);

str_score = GUI.TextField(newRect(60, 45, 160, 20), str_score);

//发送Http的POST请求

if(GUI.Button(newRect(120,80,100,25),"发送请求"))

{

StartCoroutine(TestPost());

}

this.str_response = GUI.TextArea(newRect(10, 150, 210, 100),this.str_response);

}

}

4,运行。效果如下:

SouthEast

5,点击,发送POST请求,并显示服务器返回的结果:

SouthEast

附注A:

下面是对应的web服务器端代码:

本例的web服务器,使用的是python的django,使用方法可以参加我上一篇文章:【新版django1.6的Hello world】

views代码如下:

#! /usr/bin/env python

#coding=utf-8

fromdjango.httpimportHttpResponse

deftest_post(request):

fanhui = u'服务器返回:\n'+ u'用户UI:'+ unicode(request.POST['uid']) +'\n'

fanhui = fanhui + u'分数:'+ unicode(request.POST['score'])

returnHttpResponse(fanhui)

附注B:

如果使用django,注意要把中间件里的:

'django.middleware.csrf.CsrfViewMiddleware', 注释掉。否则请求会因为CSRF机制,给拦下,报403错误。

或者干脆禁用中间件,也行。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值