sudo ufw allow 3306/tcp
sudo service ufw restart
bind-address 127.0.0.1
sudo /etc/init.d/mysql restart
Computer has revolutionized the world and programming is the most important tool to make our dream come true;
sudo ufw allow 3306/tcp
sudo service ufw restart
bind-address 127.0.0.1
sudo /etc/init.d/mysql restart
#安装Redis服务器端
~ sudo apt-get install redis-server
# 检查Redis服务器系统进程
~ ps -aux|grep redis
redis 4162 0.1 0.0 10676 1420 ? Ss 23:24 0:00 /usr/bin/redis-server /etc/redis/redis.conf
conan 4172 0.0 0.0 11064 924 pts/0 S+ 23:26 0:00 grep --color=auto redis
# 通过启动命令检查Redis服务器状态
~ netstat -nlt|grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN
# 通过启动命令检查Redis服务器状态
~ sudo /etc/init.d/redis-server status
redis-server is running
~ redis-cli
redis 127.0.0.1:6379>
# 命令行的帮助
redis 127.0.0.1:6379> help
redis-cli 2.2.12
Type: "help @" to get a list of commands in
"help " for help on
"help " to get a list of possible help topics
"quit" to exit
# 查看所有的key列表
redis 127.0.0.1:6379> keys *
(empty list or set)
# 增加一条记录key1
redis 127.0.0.1:6379> set key1 "hello"
OK
# 打印记录
redis 127.0.0.1:6379> get key1
"hello"
# 增加一条数字记录key2
set key2 1
OK
# 让数字自增
redis 127.0.0.1:6379> INCR key2
(integer) 2
redis 127.0.0.1:6379> INCR key2
(integer) 3
# 打印记录
redis 127.0.0.1:6379> get key2
"3"
# 增加一个列表记录key3
redis 127.0.0.1:6379> LPUSH key3 a
(integer) 1
# 从左边插入列表
redis 127.0.0.1:6379> LPUSH key3 b
(integer) 2
# 从右边插入列表
redis 127.0.0.1:6379> RPUSH key3 c
(integer) 3
# 打印列表记录,按从左到右的顺序
redis 127.0.0.1:6379> LRANGE key3 0 3
1) "b"
2) "a"
3) "c"
# 增加一个哈希记表录key4
redis 127.0.0.1:6379> HSET key4 name "John Smith"
(integer) 1
# 在哈希表中插入,email的Key和Value的值
redis 127.0.0.1:6379> HSET key4 email "abc@gmail.com"
(integer) 1
# 打印哈希表中,name为key的值
redis 127.0.0.1:6379> HGET key4 name
"John Smith"
# 打印整个哈希表
redis 127.0.0.1:6379> HGETALL key4
1) "name"
2) "John Smith"
3) "email"
4) "abc@gmail.com"
# 增加一条哈希表记录key5,一次插入多个Key和value的值
redis 127.0.0.1:6379> HMSET key5 username antirez password P1pp0 age 3
OK
# 打印哈希表中,username和age为key的值
redis 127.0.0.1:6379> HMGET key5 username age
1) "antirez"
2) "3"
# 打印完整的哈希表记录
redis 127.0.0.1:6379> HGETALL key5
1) "username"
2) "antirez"
3) "password"
4) "P1pp0"
5) "age"
6) "3"
# 查看所有的key列表
redis 127.0.0.1:6379> keys *
1) "key2"
2) "key3"
3) "key4"
4) "key5"
5) "key1"
# 删除key1,key5
redis 127.0.0.1:6379> del key1
(integer) 1
redis 127.0.0.1:6379> del key5
(integer) 1
# 查看所有的key列表
redis 127.0.0.1:6379> keys *
1) "key2"
2) "key3"
3) "key4"
~ sudo vi /etc/redis/redis.conf
#取消注释requirepass
requirepass redisredis
~ sudo vi /etc/redis/redis.conf
#注释bind
#bind 127.0.0.1
~ sudo /etc/init.d/redis-server restart
Stopping redis-server: redis-server.
Starting redis-server: redis-server.
~ redis-cli
redis 127.0.0.1:6379> keys *
(error) ERR operation not permitted
~ redis-cli -a redisredis
redis 127.0.0.1:6379> keys *
1) "key2"
2) "key3"
3) "key4"
检查Redis服务器占用端口
~ netstat -nlt|grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN
~ redis-cli -a redisredis -h 192.168.1.199
redis 192.168.1.199:6379> keys *
1) "key2"
2) "key3"
3) "key4"
krizna@leela:~$ sudo apt-get updatekrizna@leela:~$ sudo apt-get install vsftpdwrite_enable=YES
local_umask=022» Uncomment the below line (line no: 120 ) to prevent access to the other folders outside the Home directory.chroot_local_user=YESand add the following line at the end.allow_writeable_chroot=YES» Add the following lines to enable passive mode.pasv_enable=Yes
pasv_min_port=40000
pasv_max_port=40100krizna@leela:~$ sudo service vsftpd restartkrizna@leela:~$ sudo useradd -m john -s /usr/sbin/nologin
krizna@leela:~$ sudo passwd john/usr/sbin/nologinQueue模块实现了多生产者多消费者队列, 尤其适合多线程编程.Queue类中实现了所有需要的锁原语(这句话非常重要), Queue模块实现了三种类型队列:import Queue
#类
Queue.Queue(maxsize = 0) #构造一个FIFO队列,maxsize设置队列大小的上界, 如果插入数据时, 达到上界会发生阻塞, 直到队列可以放入数据. 当maxsize小于或者等于0, 表示不限制队列的大小(默认)
Queue.LifoQueue(maxsize = 0) #构造一LIFO队列,maxsize设置队列大小的上界, 如果插入数据时, 达到上界会发生阻塞, 直到队列可以放入数据. 当maxsize小于或者等于0, 表示不限制队列的大小(默认)
Queue.PriorityQueue(maxsize = 0) #构造一个优先级队列,,maxsize设置队列大小的上界, 如果插入数据时, 达到上界会发生阻塞, 直到队列可以放入数据. 当maxsize小于或者等于0, 表示不限制队列的大小(默认). 优先级队列中, 最小值被最先取出
#异常
Queue.Empty #当调用非阻塞的get()获取空队列的元素时, 引发异常
Queue.Full #当调用非阻塞的put()向满队列中添加元素时, 引发异常
提供公共的方法Queue.empty() #如果队列为空, 返回True(注意队列为空时, 并不能保证调用put()不会阻塞); 队列不空返回False(不空时, 不能保证调用get()不会阻塞)
Queue.full() #如果队列为满, 返回True(不能保证调用get()不会阻塞), 如果队列不满, 返回False(并不能保证调用put()不会阻塞)
Queue.put(item[, block[, timeout]]) #向队列中放入元素, 如果可选参数block为True并且timeout参数为None(默认), 为阻塞型put(). 如果timeout是正数, 会阻塞timeout时间并引发Queue.Full异常. 如果block为False为非阻塞put
Queue.put_nowait(item) #等价于put(itme, False)
Queue.get([block[, timeout]]) #移除列队元素并将元素返回, block = True为阻塞函数, block = False为非阻塞函数. 可能返回Queue.Empty异常
Queue.get_nowait() #等价于get(False)
Queue.task_done() #在完成一项工作之后,Queue.task_done()函数向任务已经完成的队列发送一个信号
Queue.join() #实际上意味着等到队列为空,再执行别的操作
def worker():
while True:
item = q.get()
do_work(item)
q.task_done()
q = Queue()
for i in range(num_worker_threads):
t = Thread(target=worker)
t.daemon = True
t.start()
for item in source():
q.put(item)
q.join() # block until all tasks are done
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import threading
import time
import Queue
SHARE_Q = Queue.Queue() #构造一个不限制大小的的队列
_WORKER_THREAD_NUM = 3 #设置线程个数
class MyThread(threading.Thread) :
def __init__(self, func) :
super(MyThread, self).__init__()
self.func = func
def run(self) :
self.func()
def worker() :
global SHARE_Q
while not SHARE_Q.empty():
item = SHARE_Q.get() #获得任务
print "Processing : ", item
time.sleep(1)
def main() :
global SHARE_Q
threads = []
for task in xrange(5) : #向队列中放入任务
SHARE_Q.put(task)
for i in xrange(_WORKER_THREAD_NUM) :
thread = MyThread(worker)
thread.start()
threads.append(thread)
for thread in threads :
thread.join()
if __name__ == '__main__':
main()
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 多线程爬取豆瓣Top250的电影名称
import urllib2, re, string
import threading, Queue, time
import sys
reload(sys)
sys.setdefaultencoding('utf8')
_DATA = []
FILE_LOCK = threading.Lock()
SHARE_Q = Queue.Queue() #构造一个不限制大小的的队列
_WORKER_THREAD_NUM = 3 #设置线程的个数
class MyThread(threading.Thread) :
def __init__(self, func) :
super(MyThread, self).__init__() #调用父类的构造函数
self.func = func #传入线程函数逻辑
def run(self) :
self.func()
def worker() :
global SHARE_Q
while not SHARE_Q.empty():
url = SHARE_Q.get() #获得任务
my_page = get_page(url) #爬取整个网页的HTML代码
find_title(my_page) #获得当前页面的电影名
time.sleep(1)
SHARE_Q.task_done()
无法保证数据的顺序性, 因为线程是并发的, 思考的方法是: 设置一个主线程进行管理, 然后他们的线程工作
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import threading
import time
import Queue
SHARE_Q = Queue.Queue() #构造一个不限制大小的的队列
_WORKER_THREAD_NUM = 3 #设置线程的个数
class MyThread(threading.Thread) :
"""
doc of class
Attributess:
func: 线程函数逻辑
"""
def __init__(self, func) :
super(MyThread, self).__init__() #调用父类的构造函数
self.func = func #传入线程函数逻辑
def run(self) :
"""
重写基类的run方法
"""
self.func()
def do_something(item) :
"""
运行逻辑, 比如抓站
"""
print item
def worker() :
"""
主要用来写工作逻辑, 只要队列不空持续处理
队列为空时, 检查队列, 由于Queue中已经包含了wait,
notify和锁, 所以不需要在取任务或者放任务的时候加锁解锁
"""
global SHARE_Q
while True :
if not SHARE_Q.empty():
item = SHARE_Q.get() #获得任务
do_something(item)
time.sleep(1)
SHARE_Q.task_done()
def main() :
global SHARE_Q
threads = []
#向队列中放入任务, 真正使用时, 应该设置为可持续的放入任务
for task in xrange(5) :
SHARE_Q.put(task)
#开启_WORKER_THREAD_NUM个线程
for i in xrange(_WORKER_THREAD_NUM) :
thread = MyThread(worker)
thread.start() #线程开始处理任务
threads.append(thread)
for thread in threads :
thread.join()
#等待所有任务完成
SHARE_Q.join()
if __name__ == '__main__':
main()
Scrapy框架(Scrapy 使用了 Twisted 异步网络库来处理网络通讯){
"pageInfo": {
"pageName": "abc",
"pagePic": "http://example.com/content.jpg"
},
"posts": [
{
"post_id": "123456789012_123456789012",
"actor_id": "1234567890",
"picOfPersonWhoPosted": "http://example.com/photo.jpg",
"nameOfPersonWhoPosted": "Jane Doe",
"message": "Sounds cool. Can't wait to see it!",
"likesCount": "2",
"comments": [],
"timeOfPost": "1234567890"
}
]
}
Java code :
JSONObject obj = new JSONObject(responsejsonobj);
String pageName = obj.getJSONObject("pageInfo").getString("pageName");
JSONArray arr = obj.getJSONArray("posts");
for (int i = 0; i < arr.length(); i++)
{
String post_id = arr.getJSONObject(i).getString("post_id");
......etc
}
from bottle import route, request, run
import os
@route('/fileselect')
def fileselect():
return '''
<form action="/upload" method="post" enctype="multipart/form-data">
Category: <input type="text" name="category" />
Select a file: <input type="file" name="upload" multiple />
<input type="submit" value="Start upload" />
</form>
'''
@route('/upload', method='POST')
def do_upload():
category = request.forms.get('category')
upload = request.files.get('upload')
print dir(upload)
name, ext = os.path.splitext(upload.filename)
if ext not in ('.png','.jpg','.jpeg'):
return 'File extension not allowed.'
#save_path = get_save_path_for_category(category)
save_path = "/home/user/bottlefiles"
upload.save(save_path) # appends upload.filename automatically
return 'OK'
run(host='localhost', port=80)