MySQL-python for Windows Distributions http://www.codegood.com/downloads python下载地址被墙了,需要vpn,到ftp里面下载就不需要了: http://www.python.org/ftp/python/ python 2.6.7没有for windows版本。

Continue

昨天晚上十一点,硬盘挂了。其实是有预兆的,从放假来了以后电脑非常卡,开始以为长时间没还原系统的事,也懒得还原。还有几次直接死机不动了。 昨晚硬盘不好用也不是突然的,一分钟前,特别卡,那时正在写django代码,调试什么的东西都开着,然后就听着主机里面像捏小泡一样的声音啪啪啪啪的,然后就卡的不动了,一分两分钟的动一下。我就慢慢结束进程,结束一个提示**.exe什么什么错误,explorer进程也自己结束了,我重启explorer进程也不行。然后启动cmd.exe也不行,提示错误,最后以为是中了什么新的exe病毒,这几天电脑卡就经常关杀毒软件。重启机器,悲剧的我开到启动画面不动了。对了,在操作当中,主机里的啪啪时常有。 第二天卸开机箱发现时硬盘啪啪的,我去。抱去修理,说硬盘挂了,我的数据哇,当时真的好怕哇。好在能把数据读出来拷到另一个盘里了。然后弄了个wd的黑盘,数据安全很重要,哈哈。有时间把重要内容拷到网盘中备份一份,预防着。

Continue

查了些验证码的资料,选了一个比较简单的例子,参照着写得不是很好。找了一篇比较经典的收藏了。

TFontAngle=range($this->TFontAngle[0],$this->TFontAngle[1]);
		$this->TFontSize=range($this->TFontSize[0],$this->TFontSize[1]);

		$arr=array();
		$Chars=$this->Chars;
		$TFontAngle=$this->TFontAngle;
		$TFontSize=$this->TFontSize;
		$FontColors=$this->FontColors;
		$code="";
		$font=dirname(__FILE__)."/font/".$this->TFonts[0];

		$charlen=strlen($Chars)-1;
		$anglelen=count($TFontAngle)-1; // 角度范围
		$fontsizelen=count($TFontSize)-1; // 角度范围
		$fontcolorlen=count($FontColors)-1; // 角度范围

		for($i=0;$i<$this->Length;$i++) ///得到字符与颜色
		{
			$char=$Chars[rand(0,$charlen)]; ///得到字符
			$angle=$TFontAngle[rand(0,$anglelen)]; ///旋转角度
			$fontsize=$TFontSize[rand(0,$fontsizelen)]; ///字体大小
			$fontcolor=$FontColors[rand(0,$fontcolorlen)]; ///字体大小

			$bound=$this->_calculateTextBox($fontsize,$angle,$font,$char); ///得到范围

			$arr[]=array($fontsize,$angle,$fontcolor,$char,$font,$bound);  ///得到矩形框
			$code.=$char;
		}
		$this->Code=$arr; //验证码
		return $code;
	}

	public function Draw() ///画图
	{
		if(empty($this->Code)) $this->RandRSI();
		$codes=$this->Code; ///用户验证码


		$wh=$this->_getImageWH($codes);

		$width=$wh[0];
		$height=$wh[1]; ///高度

		$this->Width=$width;
		$this->Height=$height;

		$this->Image = imageCreate( $width, $height );
		$image=$this->Image;

		$back = $this->_getColor2($this->_getColor( $this->BgColor)); ///背景颜色
		imageFilledRectangle($image, 0, 0, $width, $height, $back); ///填充背景

		$TPadden=$this->TPadden;

		$basex=$this->Txbase;
		$color=null;
		foreach ($codes as $v) ///逐个画字符
		{
			$bound=$v[5];
			$color=$this->_getColor2($this->_getColor($v[2]));
			imagettftext($image, $v[0], $v[1], $basex, $bound['height'],$color , $v[4], $v[3]);
			$basex=$basex+$bound['width']*$TPadden-$bound['left'];///计算下一个左边距
		}
		$this->TLine?$this->_wirteSinLine($color,$basex):null; ///画干扰线
		header("Content-type: image/png");
		imagepng( $image);
		imagedestroy($image);

	}

	/**
	 *通过字体角度得到字体矩形宽度*
	 *
	 * @param int $font_size 字体尺寸
	 * @param float $font_angle 旋转角度
	 * @param string $font_file 字体文件路径
	 * @param string $text 写入字符
	 * @return array 返回长宽高
	 */
	private function _calculateTextBox($font_size, $font_angle, $font_file, $text) {
		$box = imagettfbbox($font_size, $font_angle, $font_file, $text);

		$min_x = min(array($box[0], $box[2], $box[4], $box[6]));
		$max_x = max(array($box[0], $box[2], $box[4], $box[6]));
		$min_y = min(array($box[1], $box[3], $box[5], $box[7]));
		$max_y = max(array($box[1], $box[3], $box[5], $box[7]));

		return array(
		'left' => ($min_x >= -1) ? -abs($min_x + 1) : abs($min_x + 2),
		'top' => abs($min_y),
		'width' => $max_x - $min_x,
		'height' => $max_y - $min_y,
		'box' => $box
		);
	}

	private function  _getColor( $color ) //#ffffff
	{
		return array(hexdec($color[1].$color[2]),hexdec($color[3].$color[4]),hexdec($color[5].$color[6]));
	}

	private function  _getColor2( $color ) //#ffffff
	{
		return imagecolorallocate ($this->Image, $color[0], $color[1], $color[2]);
	}

	private function _getImageWH($data)
	{
		$TPadden=$this->TPadden;
		$w=$this->Txbase;
		$h=0;
		foreach ($data as $v)
		{
			$w=$w+$v[5]['width']*$TPadden-$v[5]['left'];
			$h=$h>$v[5]['height']?$h:$v[5]['height'];
		}
		return array(max($w,$this->Width),max($h,$this->Height));
	}

	//画正弦干扰线
	private function _wirteSinLine($color,$w)
	{
		$img=$this->Image;

		$h=$this->Height;
		$h1=rand(-5,5);
		$h2=rand(-1,1);
		$w2=rand(10,15);
		$h3=rand(4,6);

		for($i=-$w/2;$i<$w/2;$i=$i+0.1)
		{
			$y=$h/$h3*sin($i/$w2)+$h/2+$h1;
			imagesetpixel($img,$i+$w/2,$y,$color);
			$h2!=0?imagesetpixel($img,$i+$w/2,$y+$h2,$color):null;
		}
	}
}
DEMO:
$rsi = new Utils_Caption();
$rsi->TFontSize=array(15,17);
$rsi->Width=50;
$rsi->Height=25;
$code = $rsi->RandRSI();
session_start();
$_SESSION["CHECKCODE"] = $code;
$rsi->Draw();
以上代码下载地址是:http://files.cnblogs.com/chengmo/caption_chengmo.zip 作者:chengmo QQ:8292669 出处:http://www.cnblogs.com/chengmo 本文版权归作者和博客园共有,欢迎转载,请务必添加原文链接。

Continue

下面是从网上找的的,先记录: 在apache1.3.x中, 使用gzip来对内容进行压缩. 在新版的apache2.x里, deflate模块代替了gzip模块,用于对内容进行压缩. 查看Apache是否有deflate这个模块,目录:/etc/apache2/mods-available/ 启用这个mod:sudo a2enmod deflate 编辑deflate的配置文件: /etc/apache2/mods-available/deflate.conf 官网:http://httpd.apache.org/docs/2.0/mod/mod_deflate.html 一篇关于Apache优化的文章:http://my.oschina.net/lamp2me/blog/15317 默认Apache是所有插件都启用了,在配置文件apache2中可以看到,就不用启用了。

Continue

apt-cache show python-imaging 查看版本是最新的1.1.7 直接apt-get install python-imaging 安装成功 import Image Image.VERSION '1.1.7' 查资料可以自己下载源码编译,但是我没选择折腾。直接安装吧。 再记录一个小问题:在Ubuntu下django的settings的配置中TIME_ZONE = 'CCT'不成功,需要修改为'Asia/Shanghai'。就OK了,昨天忘记记录了。

Continue

上传到Ubuntu服务器后错误百出,报错Data truncated for column 'title' at row 1, Google搜索发现是编码不统一的问题。 查看数据库编码的方法: show variables like 'character%'; 修改/etc/mysql/my.cnf文件 找到客户端配置[client] 在下面添加 default-character-set=utf8 默认字符集为utf8 在找到[mysqld] 添加 default-character-set=utf8 默认字符集为utf8 init_connect='SET NAMES utf8' (设定连接mysql数据库时使用utf8编码,以让mysql数据库为utf8运行) 重启mysql服务:service mysql restart 再查看发现成为utf8了。 drop掉原来建立的数据库,新建一个utf8的: CREATE DATABASE ms_db CHARACTER SET utf8 ; 前台测试,不再报错。

Continue

ExtractionError: Can't extract file(s) to egg cache The following error occurred while trying to extract file(s) to the Python egg cache: [Errno 13] Permission denied: '/var/www/.python-eggs' The Python egg cache directory is currently set to: /var/www/.python-eggs Perhaps your account does not have write access to this directory? You can change the cache directory by setting the PYTHON_EGG_CACHE environment variable to point to an accessible directory. 解决办法: mkdir /var/www/.python-eggs chmod -R 777 /tmp/.python-eggs 设置PYTHON_EGG_CACHE环境变量 $ SetEnv PYTHON_EGG_CACHE /tmp/aaa/ ##(setenv是在cshell中用的,在bshell中用的是 export 不同的SHELL设置变量的方法很不一致: csh : set、setenv bash : set、export ksh : set、export或直接赋值 vim /etc/profile export PYTHON_EGG_CACHE=/tmp/.python-eggs source /etc/profile export PYTHON_EGG_CACHE=/tmp/.python-eggs env |grep egg chmod -R 777 /tmp/.python-eggs or # vi /root/.bashrc export PYTHON_EGG_CACHE=/tmp/.python-eggs ) 目录权限注意要是apache用户,或者简单点就777

Continue

>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2012, 2, 14, 16, 24, 36, 171000)
>>> dir(now)
发现取得时间方法是now.year的方式。
>>> now2 = datetime.datetime.now()
>>> dd=now2-now
>>> dd
datetime.timedelta(0, 209, 938000)
>>> dir(dd)
>>> dd.days
0
>>> dd.microseconds
938000
>>> dd.seconds
209
MD5加密方法:
>>> import md5
>>> v = md5.new('uuuuu').hexdigest()
>>> v
'3c9aa281ced92294f259c0c55520b2cf'
>>> b= md5.new('uuuuu').hexdigest()[16:]
>>> b
'f259c0c55520b2cf'

Continue

用户注册需要邮箱的验证,找回密码需要邮箱。我选择用gmail邮箱来发送邮件,配置如下: settings.py文件中添加: #邮箱设置 EMAIL_HOST = 'smtp.gmail.com' #EMAIL_PORT = '465' EMAIL_HOST_USER = 'name@gmail.com' EMAIL_HOST_PASSWORD = 'password' EMAIL_USE_TLS = True 端口的设置,可能是默认的25,开始没有成功。 发送邮件代码: from django.core.mail import send_mail send_mail('subject','body','name@gmail.com', ['to_some@mail.com'],fail_silently=True) 非常简单。

Continue

用户登录的时候,选择是否记住登录状态,就是所谓的自动登录。又是从官网上找到了,兴奋ing~ django官网的文档很好,很详细。 Browser-length sessions vs. persistent sessions一节讲django的记录用户登录的机制。 settings文件里的SESSION_EXPIRE_AT_BROWSER_CLOSE为True时,在用户关闭浏览器时,登录状态就会失效。 为False是,用户关闭浏览器后,下次打开浏览器不需要重新登录。 如果需要用户能够哦你各种这个功能,我们需要借助request.session的方法set_expiry()。 set_expiry(VALUE)设置:   1.如果是个整数,表示帐号的存活时间。request.session.set_expiry(300) 2.VALUE还可以是个datetime or timedelta。 3.如果是0,则会在关闭浏览器时失效。 4.可以为None。 只需要在登录页面上加入一个checkbox,再在views里进行判断就Ok了。



Continue