Skip to content

Commit 30306d0

Browse files
committed
TCP/IP
1 parent 87af006 commit 30306d0

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

.settings/org.eclipse.core.resources.prefs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
eclipse.preferences.version=1
2+
encoding/Collections\u96C6\u5408\u6A21\u5757.py=utf-8
23
encoding/IO\u7F16\u7A0B.py=utf-8
4+
encoding/TCPIP.py=utf-8
35
encoding/\u5206\u5E03\u5F0F\u8FDB\u7A0B.py=utf-8
46
encoding/\u591A\u7EBF\u7A0B.py=utf-8
57
encoding/\u591A\u8FDB\u7A0B.py=utf-8

Collections集合模块.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# namedtuple()函数创建自定义的tuple
5+
from collections import namedtuple
6+
Point = namedtuple('Point', ['x', 'y'])
7+
p = Point(101, 2)
8+
print p.x
9+
print p.y
10+
11+
# deque双端队列
12+
from collections import deque
13+
q = deque(['a', 'b', 'c'])
14+
q.append('x')
15+
q.appendleft('y')
16+
print q
17+
18+
# defaultdict 无key时返回默认值
19+
from collections import defaultdict
20+
d = defaultdict(lambda : 'N/A')
21+
d['key1'] = 'abc'
22+
print d['key1']
23+
print d['key2']
24+
25+
# OrderedDict 顺序key字典
26+
from collections import OrderedDict
27+
od = OrderedDict()
28+
od['oz'] = 2
29+
od['oc'] = 3
30+
od['oa'] = 1
31+
print od.keys()
32+
33+
# Counter 简单计数器
34+
from collections import Counter
35+
c = Counter()
36+
for ch in 'programming':
37+
c[ch] += 1
38+
print c
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+

TCPIP.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import socket
5+
import threading, time
6+
7+
type = 'client'
8+
if type == 'client':
9+
# 创建socket AF_INET IPV4 AF_INET6 IPV6
10+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
11+
# 建立连接:
12+
s.connect(('192.168.1.108', 9999))
13+
# 接收欢迎消息:
14+
print s.recv(1024)
15+
for data in ['Michael', 'Tracy', 'Sarah']:
16+
# 发送数据:
17+
s.send(data)
18+
print s.recv(1024)
19+
s.send('exit')
20+
s.close()
21+
22+
else:
23+
def tcplink(sock, addr):
24+
print 'Accept new connect from %s:%s...' % addr
25+
sock.send('Welcome')
26+
while True:
27+
data = sock.recv(1024)
28+
time.sleep(1)
29+
if data == 'exit' or not data:
30+
break
31+
sock.send('Hello %s' % data)
32+
sock.close()
33+
print 'Connection from %s:%s closed' % addr
34+
35+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
36+
# 监听端口
37+
s.bind(('0.0.0.0', 9999))
38+
s.listen(5)
39+
print 'waiting for connect...'
40+
while True:
41+
# 接收一个新连接
42+
sock, addr = s.accept()
43+
# 创建新线程处理
44+
t = threading.Thread(target = tcplink, args = (sock, addr))
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+

0 commit comments

Comments
 (0)