王筝的博客
ruby学习

$mail

选择对应的数字查看邮件

File "/opt/app/python/tv-cms-for-ch/current/scripts/sync_cms_for_ch.py",
line 4, in ?
from ConnectionParams import *
File
"/opt/app/python/tv-cms-for-ch/releases/20140526054935/scripts/ConnectionParams.py",
line 3, in ?
from pymongo import MongoClient
ImportError: No module named pymongo
没有pymongo的原因是系统装了两个python,其中有一个版本没有装pymongo,执行
crontab的时候需要指定一下版本所在 的位置

15:23 [root@a02.cmsapi.ott.b28.tudou]$ whereis python
python: /usr/bin/python /usr/bin/python2.4 /usr/lib/python2.4
/usr/local/bin/python2.7-config /usr/local/bin/python2.7
/usr/local/bin/python /usr/local/lib/python2.7 /usr/local/lib/python2.4
/usr/include/python2.4 /usr/share/man/man1/python.1.gz
tty:[0] jobs:[0] cwd:[/opt/app/python]
15:23 [root@a02.cmsapi.ott.b28.tudou]$ python -c 'import sys;print sys.path'
['',
'/usr/local/lib/python2.7/site-packages/setuptools-3.5.1-py2.7.egg',
'/usr/local/lib/python2.7/site-packages/pymongo-2.6_-py2.7-linux-x86_64.egg',
'/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg',
'/usr/local/lib/python2.7/site-packages/python_memcached-1.53-py2.7.egg', '/usr/local/lib/python27.zip',
'/usr/local/lib/python2.7', '/usr/local/lib/python2.7/plat-linux2',
'/usr/local/lib/python2.7/lib-tk', '/usr/local/lib/python2.7/lib-old',
'/usr/local/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/site-packages']
tty:[0] jobs:[0] cwd:[/opt/app/python]
15:24 [root@a02.cmsapi.ott.b28.tudou]$ crontab -e

MAILTO=yang@a.com,wang@y.com

#同步专题脚本
0 */1 * * * /usr/local/bin/python
/opt/app/python/tv-cms-for-ch/current/scripts/sync_cms_for_ch.py

 2067  2014-06-04 15:14:41:crontab -e
 2068  2014-06-04 15:15:17:whereis python
 2070  2014-06-04 15:15:37:python -c 'import sys;print sys.path'
 2071  2014-06-04 15:16:03:history

 

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()

 

 

 

刚开始学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′)