Skip to content

Commit 969223d

Browse files
committed
selectors and non-blocking socket
1 parent 16aee46 commit 969223d

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

studyLib/socket/my_tcp_client.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22
# -*- coding: utf-8 -*-
33

44
import socket
5+
import time
56

67
__author__ = 'Mr.Huo'
78

89

9-
1010
def main():
11-
server_addr = ('146.11.22.166', 20000)
11+
server_addr = ('146.11.22.150', 20000)
1212
client = socket.socket()
1313
client.connect(server_addr)
1414
send_data1 = [b'1aheuo\r\n', b'2ashxao\r\n', b'3bhaua\r\n']
1515
print(client.getpeername())
16-
for data in send_data1:
17-
client.send(data)
18-
print(client.recv(1024))
19-
client.send(b'exit\r\n')
16+
while True:
17+
for data in send_data1:
18+
client.send(data)
19+
print(client.recv(1024))
20+
time.sleep(2)
2021
client.close()
2122
pass
2223

studyLib/socket/network_setting.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@
1010
UDP_PORT_C = 10002
1111
TCP_PORT_S = 20000
1212
TCP_PORT_C = 20002
13+
14+
TcpSerAddr = (LOCALIP, TCP_PORT_S)
15+
TcpCliAddr = (LOCALIP, TCP_PORT_C)
16+
UdpSerAddr = (LOCALIP, UDP_PORT_S)
17+
UdpCliAddr = (LOCALIP, UDP_PORT_C)

studyLib/socket/sockServer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ def main():
2929
# TCPServer,IO多路复用,但是StreamRequestHandler还是同步阻塞IO
3030
# 同步阻塞IO、同步非阻塞IO、IO多路复用、异步IO
3131
tcpServ = TCP(TcpSerAddr, MyRequestHandler)
32+
print(tcpServ.fileno())
3233
with selectors.SelectSelector() as selector:
3334
key = selector.register(tcpServ,selectors.EVENT_READ)
35+
ready = selector.select()
36+
print(ready)
3437
fd = selector._fileobj_lookup(tcpServ)
3538
fd1 = selectors._fileobj_to_fd(tcpServ)
3639
print(tcpServ.fileno())
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
import selectors, socket
5+
from time import ctime
6+
from network_setting import *
7+
8+
__author__ = 'Mr.Huo'
9+
10+
sel = selectors.DefaultSelector()
11+
12+
13+
def accept(sock, mask):
14+
conn, addr = sock.accept()
15+
print('Accept %s from %s' % (conn, addr))
16+
#conn.setblocking(False)
17+
sel.register(conn, selectors.EVENT_READ, read)
18+
19+
20+
def read(conn, mask):
21+
data = conn.recv(1000)
22+
if data:
23+
print('Recv data from [%s]. Data is [%s]' % (conn.getpeername(), data))
24+
#send_data = ('[' + ctime() + '] ').encode() + data
25+
send_data = data
26+
try:
27+
conn.sendall(send_data)
28+
except BlockingIOError:
29+
print('10035')
30+
else:
31+
print('Closing ', conn)
32+
sel.unregister(conn)
33+
conn.close()
34+
35+
36+
def main():
37+
sock = socket.socket()
38+
sock.bind(TcpSerAddr)
39+
sock.listen(100)
40+
sock.setblocking(False)
41+
sel.register(sock, selectors.EVENT_READ, accept)
42+
43+
while True:
44+
events = sel.select()
45+
print(events)
46+
for key, mask in events:
47+
callback = key.data
48+
callback(key.fileobj,mask)
49+
50+
51+
if __name__ == '__main__':
52+
main()

0 commit comments

Comments
 (0)