-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
152 lines (109 loc) · 3.08 KB
/
test.py
File metadata and controls
152 lines (109 loc) · 3.08 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# -*- coding: utf-8 -*-
#python字典格式
dic = {'name':'lmm','age':25,'set':'man'}
print(dic['set'])
#定义函数
def num(x):
if x > 8:
print "大于8"
else:
print "小于8"
num(4)
#定义类
class Student(object):
def __init__(self,name,score):
self.name = name
self.score = score
def getName(self):
return self.__name
def getScore(self):
return self.__score
student = Student('lmm',10)
# print student.getName()
# print student.getScore()
# student.__score = 1
print student.score
print type(student)
class Class(object):
name = "jack"
def __init__(self):
self.name = 'alice'
instance = Class()
print Class.name #jack
# del Class.name 可以删除类属性,也可以删除实例属性
print instance.name #alice
print Class.name #AttributeError: type object #Class' has no attribute 'name'
from datetime import datetime
now = datetime.now() # 获取当前datetime
print(now)
dt = datetime(2015, 4, 19, 12, 20) # 用指定日期时间创建datetime
print(dt)
import pdb
# 导入socket库:
import socket
# 创建一个socket:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 建立连接:
s.connect(('localhost', 80))
# 模拟get发送数据:
# s.send(b'GET /lmm/test.php HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n')
#模拟post发送数据
str = ("POST /lmm/test.php HTTP/1.1\r\n"
"Host: localhost\r\n"
"User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0\r\n"
# "Referer: http://www.xxxxx.com/\r\n"
"Content-Length: 17\r\n"
"Cookie: session=f28edeacd71e759d05a80bb3cd9c91856952b3e3%7E58eb987a9efb26-19897462\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Connection: keep-alive\r\n\r\n"
"param1=a¶m2=b"
)
s.send(str)
# 接收数据:
buffer = []
while True:
# 每次最多接收1k字节:
d = s.recv(2048)
if d:
buffer.append(d)
else:
break
data = b''.join(buffer)
# 关闭连接:
s.close()
# 打印数据
print(data)
#python 读写文件操作
#读打开文件
# f = open("open.txt",'r')
#写打开文件
# f = open("open.txt",'w')
#读取文件内容到内存
# content = f.read()
#写内容到文件
# str = "this is a test content to write to txt file"
# f.write(str)
# f.close();
#简化版读操作
# with open('open.txt','r') as f:
# print(f.read())
# #简化版写操作
# with open('open.txt','w') as f:
# str = "continue write content to file"
# print(f.write(str))
#逐行读取文件内容
with open('open.txt','r') as f:
for line in f.readlines():
pdb.set_trace()#打断点
print(line)
#json字符串与字典格式的转化
import json
dic = {"name":"lmm","age":25,"set":"man"}
print(type(dic))
print(type(json.dumps(dic)))
dic = dict(one = 1,two = 12,three = 'three')
print(json.dumps(dic))
print(type(dic))
json_str = '{"name":"wo","height":123,"weight":60}';
dic = json.loads(json_str)
print(dic['name'])