王筝的博客
ruby学习

 

tcp_server.py

from socket import *

 

server = socket(AF_INET, SOCK_STREAM)

server.bind((”,12345))

server.listen(5)

print ‘wait for connect…’

while True:

client,address = server.accept()

print address,client

while True:

data = client.recv(1024)

print data

send_data =  raw_input(‘>’)

if not send_data:

break

client.send(send_data)

client.close()

server.close()

 

 

 

 

 

tcp_client.py

from socket import *

 

server = socket(AF_INET,SOCK_STREAM)

server.connect((‘127.0.0.1′,12345))

while True:

message = raw_input(‘client>’)

if not message:

break

server.send(message)

data = server.recv(1024)

print ‘recv is %s’ % data

server.close()

 

 

 

https://github.com/smartinez87/exception_notification#sections

 

Add the following line to your application’s Gemfile:

gem 'exception_notification'

As of Rails 3 ExceptionNotification is used as a rack middleware, or in the environment you want it to run. In most cases you would want ExceptionNotification to run on production. Thus, you can make it work by putting the following lines in your config/environments/production.rb:
Whatever::Application.config.middleware.use ExceptionNotification::Rack,
  :email => {
    :email_prefix => "[Whatever] ",
    :sender_address => %{"notifier" <notifier@example.com>},
    :exception_recipients => %w{exceptions@example.com}
  }

31 Cms::Application.config.middleware.use ExceptionNotifier,
32 :email_prefix => “[CMS Error] “,
33 :sender_address => %{#{Settings.notifier.full_email}},
34 :exception_recipients => %w{wangzheng@111.com}

刚开始学python,所以很多代码都需要在ipython里尝试一下。今天记录的是最基本的操作mysql数据库。

写数据库连接操作的时候,仿佛回到了当年在前两家公司写asp.net的感觉。

1.首先在mysql数据库里新建个数据库

create database db_02  default charset utf8;

create table user (id int auto_increment primary key,username char(20),email char(20));

2.开始写python代码

 

import MySQLdb

conn = MySQLdb.connect(user=’root’,host=’localhost’,passwd=”)

conn.select_db(‘db_02′)

cursor =conn.cursor()

sql = ‘insert into user(name,email) values(“andy”,”andy@163.com”)’

cursor.execute(sql)

————————————————

cursor.execute(‘insert into user(name,email) values(“andy”,”andy@163.com”)’)

查看:

In [14]: sql=’select * from user’

 

In [15]: cur.execute(sql)

Out[15]: 2L

 

In [16]: cur.fetchone()

Out[16]: (1L, ‘andy’, ‘andy@163.com’, None)

 

In [17]: cur.fetchone()

Out[17]: (2L, ‘andy’, ‘andy@163.com’, None)

In [19]: cur.execute(sql)

Out[19]: 2L

In [20]: cur.fetchall()

Out[20]: ((1L, ‘andy’, ‘andy@163.com’, None), (2L, ‘andy’, ‘andy@163.com’, None))

最后是

cursor.close()

conn.close()

写到这里认为万事大吉喽,去mysql里一看发现数据没进来,找了原因才知道这是与表的类型有关。

mysql> show create table user;

+——-+————————————————————————————–

| Table | Create Table

+——-+————————————————————————————–

| user  | CREATE TABLE `user` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`name` char(20) NOT NULL,

`email` char(20) NOT NULL,

`password` char(200) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 |

+——-+————————————————————————————–

因为用的是InnoDB,不是MyISAM, MyISAM类型不支持事务处理等高级处理,而InnoDB类型支持。MyISAM类型的表强调的是性能,其执行数度比InnoDB类型更快,但是不提供事务支持,而InnoDB提供事务支持已经外部键等高级数据库功能。MyISAM的读性能是比Innodb强不少的.

所以只要加上一句conn.commit(),数据库里就可以看到记录了

 

 

 

获取本机ip信息的命令ifconfig总是在用,这次拿到pyhton代码里,感觉python好麻烦啊,每次都要import。

可以在程序里直接read出来,也可以先在命令行执行ifconfig > xx.txt,这样可以直接访问倒入后的文本信息。

这个小程序有个问题,与朋友讨论了还没解决彻底,如果用findall是可以匹配所有的,但是怎么解决用match只匹配一次的问题呢,习惯了js里的/g,当然如果只是解决问题就好的话,推荐大家还是用findall。

from subprocess import Popen, PIPE

import re

def get_ipinfo():

f = Popen(‘ifconfig’, shell=True, stdout=PIPE)

s = f.stdout.read()

#s = file(‘ipinfo.txt’,’r’).read()

p = r’.*?inet (?P<add>.*?) ‘

p = re.compile(p, re.DOTALL)

#out = p.findall(s)

out = p.match(s).groupdict()

return out

print get_ipinfo()

正如很多同学所知道的,楼主开始学习python了,前进的道路曲曲折折,有荆棘也有陷阱,从最简单的小程序写起,每天练习,将python进行到底。

有一点比较别扭的就是python的换行之后空四个空格,ruby都是两个,并且python在方法和循环语句的第一句都要加冒号.

首先先来做一些准备工作,mysql里查看表结构以及增加列的语句:

show create table user;

alter table user add salt char(10);

md5和sha1大家可以自己选

import hashlib

In [64]: passwd=’111111′

 

In [67]: md5pass=hashlib.md5(passwd)

 

In [69]: hashlib.md5(passwd).hexdigest()

Out[69]: ’96e79218965eb72c92a549dd5a330112′

 

In [70]: hashlib.sha1(passwd).hexdigest()

Out[70]: ‘3d4f2bf07dc1be38b20cd6e46949a1071f9d0e3d’

In [71]: len(hashlib.sha1(passwd).hexdigest())

Out[71]: 40

In [72]: import string

 

In [73]: import random

In [74]: string.letters

Out[74]: ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’

 

In [75]: string.digits

Out[75]: ‘0123456789’

 

In [76]: string.digits+string.letters

Out[76]: ‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’

 

In [77]: random.sample(string.letters,5)

Out[77]: [‘S’, ‘E’, ‘w’, ‘F’, ‘L’]

In [78]: ”.join(random.sample(string.letters,10))

Out[78]: ‘RVviZAqLQK’

 

——————————————–

dbconfig ={
‘host':’localhost’,
‘user':’root’,
‘passwd':”,
‘charset':’utf8′,
‘db':’db_02′
}
import MySQLdb
import string
import random
import hashlib

class User(object):
def __init__(self, name, email, passwd,salt):
conn = MySQLdb.connect(**dbconfig)
cursor = conn.cursor()
sql = ‘insert into user(name, email, password, salt) values(%s, %s, %s, %s)’
cursor.execute(sql, (name, email, passwd, salt))
#self.name = name
#self.email = email
conn.commit()
cursor.close()
conn.close()

@staticmethod
def regist( name, email, passwd):
salt,passwd = User.salt_passwd(passwd)
User(name, email, passwd, salt)

@staticmethod
def login(name, passwdin):
conn = MySQLdb.connect(**dbconfig)
cursor = conn.cursor()
sql = ‘select id,name,email,password,salt from user where name=%s’
if cursor.execute(sql, (name,)):
userid,name, email, passwd, salt =cursor.fetchone()
#print cursor.fetchone()
salt_passwdin = hashlib.sha1(salt+passwdin).hexdigest()
if passwd == salt_passwdin:
return userid
return None

#def get_user(userid)
def get_name(self, userid):
return userid, name, email,

@staticmethod
def salt_passwd(passwd):
salt = ”.join(random.sample(string.letters, 10))
passwd = hashlib.sha1(salt+passwd).hexdigest()
return salt, passwd
testuser = {
‘name':’alen’,
‘passwd':’111111′,
‘email':’alen@a.com’
}

#User.regist(**testuser)

print User.login(name=’alenaaa’, passwdin=’11111′)

 

[root@a02]$show dbs;
changhong_tv_cms 0.078GB

[root@a02]$ mongodump -d changhong_tv_cms -o
/temp/db_mongo.20140521.mongo

/workspace/db_baks$ scp -r -P 22022
root@10.xxx.xx.xx:/temp/db_mongo_changhong.20140521.mongo .

/workspace/db_baks$ mongorestore --directoryperdb
db_mongo_changhong.20140521.mongo

$ mongodump –help

options:
–help produce help message
-v [ –verbose ] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
–version print the program’s version and exit
-h [ –host ] arg mongo host to connect to ( <set name>/s1,s2 for
sets)
–port arg server port. Can also use –host hostname:port
–ipv6 enable IPv6 support (disabled by default)
-u [ –username ] arg username
-p [ –password ] arg password
–dbpath arg directly access mongod database files in the given
path, instead of connecting to a mongod server –
needs to lock the data directory, so cannot be used
if a mongod is currently accessing the same path
–directoryperdb if dbpath specified, each db is in a separate
directory
–journal enable journaling
-d [ –db ] arg database to use
-c [ –collection ] arg collection to use (some commands)
-o [ –out ] arg (=dump) output directory or “-” for stdout
-q [ –query ] arg json query
–oplog Use oplog for point-in-time snapshotting
–repair try to recover a crashed database
–forceTableScan force a table scan (do not use $snapshot)

-h 导出源
-d 要导出的数据库名称
-o 数据库要导出的位置

1.下载安装程序

打开终端输入以下命令

$ wget http://www.djangoproject.com/m/releases/1.5/Django-1.5.4.tar.gz

 

2.把刚下载的包解压缩

$ tar zxvf Django-1.5.4.tar.gz

 

3.打开解压文件,安装

$ cd Django-1.5.4

$ python setup.py install

 

4. 测试一下安装的django的

$ python

这样就进入了python环境

$ import django

$ django.VERSION

 

详细的操作过程如下:

angela@angeladeMacBook-Air:/workspace$python

Python 2.7.5 (default, Aug 25 2013, 00:04:04)

[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin

Type “help”, “copyright”, “credits” or “license” for more information.

>>> import django

>>> django.VERSION

(1, 5, 4, ‘final’, 0)

  1. 简介

众所周知,Mac的操作系统是基于Unix的,在这个系统上大家可以安装一些专门为mac定制开发的软件。这就带来了一个问题,手工编译每个软件,手动进行依赖管理不是很方便,也不利于管理已安装的软件,所以mac中就诞生了软件管理工具Homebrew,Linux中咱们常用的蕾丝的软件管理工具有apt-get、yum等。

 

Homebrew安装软件时候,首选系统自带的各种库,减少了使得软件包的编译时间,效率很高;与此同时,也不会带来系统冗余,软件包的管理非常方便灵活。Homebrew的还有一个特点是使用Ruby定义的软件包安装配置(叫做formula),定制非常easy。

homebrew会将软件安装到独立的目录,并将文件地址软链接至/usr/local ,所有文件均会被安装到预定义目录下,所以使用之后无需担心安装位置啦。

开始用homebrew来管理依赖包吧。

2.下面咱们来安装一下homebrew吧,在终端输入以下的代码,回车之后就开始安装啦。

$ ruby -e “$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)”

mac上安装rails的时候报错,

安装rails的在终端执行了一句命令:

$sudo gem install rails

报错了,报错信息为

clang: error: unknown argument: ‘-multiply_definedsuppress’ [-Wunused-command-line-argument-hard-error-in-future]

 

完整的执行过程及报错信息如下:

 

angela@angeladeMacBook-Air:~$sudo gem install rails

Password:

Building native extensions.  This could take a while…

ERROR:  Error installing rails:

ERROR: Failed to build gem native extension.

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb

creating Makefile

make “DESTDIR=”

compiling generator.c

linking shared-object json/ext/generator.bundle

clang: error: unknown argument: ‘-multiply_definedsuppress’ [-Wunused-command-line-argument-hard-error-in-future]

clang: note: this will be a hard error (cannot be downgraded to a warning) in the future

make: *** [generator.bundle] Error 1

Gem files will remain installed in /Library/Ruby/Gems/2.0.0/gems/json-1.8.1 for inspection.

Results logged to /Library/Ruby/Gems/2.0.0/gems/json-1.8.1/ext/json/ext/generator/gem_make.out

 

原因很简单,

Xcode 5.1中的苹果编译器将不认识的命令行视为错误行为。这类报错常出现在ruby和python的安装过程中,因为这两类程序中,很多编译语句较为特殊。

 

解决方案就是在安装语句前面加上这句

ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future ,

 

以安装rails为例,使用以下的语句:

$ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future gem install rails

node.js最初是2009年发布的,目标是为聊实现事件驱动和非阻塞I/O的web服务器,应用的场景非常的广泛,有web服务器、实时应用、分布式应用、游戏、一些工具中,它是基于Chrome的V8引擎开发的。大家可以在github上找到开源代码:

http://github.com/joyent/node.

 

之前用的电脑是windows系统的,现在换了mac之后需要在mac上重新安装下,let‘s start now

 

git clone git://github.com/ry/node.git
cd node
./configure
make
sudo make install