0%

iOS插到Mac用终端抓包

ifconfig -l # 查看当前能抓包的端口
rvictl -s xxxxxxxxxxxxxx # 加入手机的Device ID
sudo tcpdump -i rvi0 -w ~/desktop/抓包/100.pcap # 存放包路径

iOS SDK开发技巧

在用XCode开发SDK的时候用好PROJECT_DIR可以免于重复手动的粘贴复制
在RunScript里面添加下面的代码

1
2
3
4
5
# copy framework to SDK/build
rm -rf "${PROJECT_DIR}/../SDK/build/${PROJECT_NAME}.framework"
cd "${CONFIGURATION_BUILD_DIR}"
cp -r "${PROJECT_NAME}.framework" "${PROJECT_DIR}/../SDK/build/"

上面的文件名按照自己的需要修改

Python Socket编程

目的: 客户端发送两个整数,让服务器给我们做加法,然后再用Json格式传给客户端

UDP编程

UDPClient.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 #coding:utf8
from socket import *
import json
serverName = '127.0.0.1'
serverPort = 12000
clentSocket = socket(AF_INET,SOCK_DGRAM)
message1 = raw_input("InputNumber1> ")
message2 = raw_input("InputNumber2> ")
d = {
"message1" : message1,
"message2" : message2
}
data = json.dumps(d)
clentSocket.sendto(data,(serverName,serverPort))
modifidMessage, serverAddress = clentSocket.recvfrom(2048)
mes = json.loads(modifidMessage)
print int(mes['sum'])
clentSocket.close()

UDPServer.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import json
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET,SOCK_DGRAM)
serverSocket.bind(('127.0.0.1',serverPort))
print "The server is ready to receive"
while True:
message, clientAddress = serverSocket.recvfrom(2048)
dic = json.loads(message)
print dic
num1 = int(dic["message1"])
num2 = int(dic["message2"])
sum = num1 + num2
data = {
"sum" : sum
}
d = json.dumps(data)
print d
serverSocket.sendto(d, clientAddress)

TCP编程

TCPClient.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# coding:utf8
from socket import *
import json
serverName = '127.0.0.1'
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort)) #创建与服务器的连接 也就是三处握手
number1 = raw_input('Input Number1:')
number2 = raw_input('Input Number2:')
d = {
'num1' : number1,
'num2' : number2
}
jsonStr = json.dumps(d)
clientSocket.send(jsonStr)
serverData = clientSocket.recv(1024)
print 'From Server: ', serverData
data = json.loads(serverData)
print int(data['sum'])
clientSocket.close()

TCPServer.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from socket import *
import json
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('127.0.0.1', serverPort))
serverSocket.listen(1)
print 'The server is ready to receive'
while True:
connectionSocket, address = serverSocket.accept()
sentence = connectionSocket.recv(1024)
jsonData = json.loads(sentence)
print jsonData
num1 = int(jsonData['num1'])
num2 = int(jsonData['num2'])
sum = num1 + num2
dic = {
'sum' : sum
}
clientData = json.dumps(dic)
connectionSocket.send(clientData)
connectionSocket.close()

Mac下配置CGI环境

最近在学Python CGI编程,环境一直弄不好,Google了一大圈,看了很多博客,后面实践下来,要么就是403,要么就是解析不出来,直接打印出源码,现在在这里记录一下我自己的操作步骤,以便以后在遇到该问题好直接上手

1.在httpd.conf文件中开启CGI配置和虚拟主机

  1. sudo vim /etc/apache2/httpd.config
  2. 取消注释 Include /private/etc/apache2/extra/httpd-vhosts.conf
  3. 取消注释 AddHandler cgi-script .cgi
  4. 取消注释 AddType text/html .shtml
  5. 取消注释 AddOutputFilter INCLUDES .shtml
  6. AddHandler cgi-script .cgi后面添加要支持的cgi文件,比如.pl .py
  7. 重启服务器 sudo apachectl restart

2.在httpd-vhosts.conf文件中设置主机目录

  1. sudo vim /etc/apache2/extra/httpd-vhosts.conf
  2. 注释掉前面的两个实例主机配置
  3. 添加下面的配置
     <VirtualHost *:80>
         DocumentRoot "/Library/WebServer/Documents" #原始的目录
         ServerName localhost
         ErrorLog "/private/var/log/apache2/localhost-error_log"
         CustomLog "/private/var/log/apache2/localhost-access_log" common
     </VirtualHost> 
     <VirtualHost *:80>
         DocumentRoot "/Users/mac/Desktop/PyCharm/cgi/" #自定义目录
         ServerName studycgi  #自定义本地域名
         ErrorLog "/private/var/log/apache2/sites-error_log"
         CustomLog "/private/var/log/apache2/sites-access_log" common
         <Directory "/Users/mac/Desktop/PyCharm/cgi/">
             Options Indexes FollowSymLinks MultiViews ExecCGI #这里必须要有ExecCGI否则会403
             AllowOverride All
             Require all granted
         </Directory> #这里添加权限设置,否则会出现403
     </VirtualHost>

3.设置username.conf

  1. etc/apache2/users/username.conf #这里的username是你的电脑名称
  2. 添加如下配置
         <Directory "/Users/mac/Desktop/PyCharm/cgi/">
             Options Indexes FollowSymLinks MultiViews ExecCGI #这里必须要有ExecCGI否则会403
             DirectoryIndex index.html index.cgi
             AllowOverride None
             Require all granted
             Order allow,deny
             Allow from all
         </Directory>
  3. 重启服务器 sudo apachectl restart

4.设置hosts文件

  1. sudo vim /etc/hosts
  2. 增加 127.0.0.1 studycgi
  3. 在浏览器打开 http://studycgi 即可访问自定义的网站目录

5. 开始就可以撸代码

…………

UIScrollView的分页效果,以及Masonry在UIScrollView中的用法

1.Masonry是基于AutoLayout的高度封装

要是你直接用官方的AutoLayout来布局的话,看看下面的代码感受一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
UIView *view = UIView.new;
view.backgroundColor = [UIColor purpleColor];
view.translatesAutoresizingMaskIntoConstraints = NO;//键值将Autoresizing转换为Constraints
[self addSubview:view];

NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:150];
[view addConstraint:width];

NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:150];
[view addConstraint:height];

NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:view.superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:100];
[view.superview addConstraint:left];

NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:10];
[view.superview addConstraint:top];

2.所以还是乖乖的用Masonry吧 :)

创建一个UIScrollView以便测试

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
UIScrollView *sc = [[UIScrollView alloc]init];
sc.backgroundColor = [UIColor orangeColor];
// sc.bounces = NO;//禁止回弹效果
sc.delegate = self;
[self addSubview:sc];
[sc mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.bottom.right.mas_equalTo(0);
make.width.equalTo(self);
make.height.mas_equalTo(200);
}];

UIView *lastView;;
for (int i = 0; i < [self randomColor].count; i ++) {
UIView *view = [[UIView alloc]init];
view.backgroundColor = [self randomColor][i];
[sc addSubview:view];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.mas_equalTo(0);
make.width.height.equalTo(sc);
// if (lastView) {
// make.left.mas_equalTo(lastView.mas_right).offset(0);
// }else{
// make.left.mas_equalTo(sc).offset(0);
// }

make.left.mas_equalTo(lastView ? lastView.mas_right : sc).offset(0);

}];
lastView = view;
}
[lastView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(sc).offset(0);
}];

3.遵守其代理方法

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
- (NSArray *)randomColor{
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor],[UIColor greenColor],[UIColor blueColor],[UIColor grayColor], nil];
// int number = arc4random() % colors.count;
// return colors[number];
return colors;
}


//分页效果
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
if (!decelerate) {
[self customPage:scrollView];
}
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
[self customPage:scrollView];
}


- (void)customPage:(UIScrollView *)sc{
CGFloat contentOffset = sc.contentOffset.x;

NSInteger index = (contentOffset + sc.frame.size.width) / self.frame.size.width - 0.5; //-0.5的目的就是为了当刚好没有划到一半时,自动划过去

[sc setContentOffset:CGPointMake(index * self.frame.size.width, 0) animated:YES];
}