forked from tomoncle/Python-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudy_socket.py
More file actions
58 lines (47 loc) · 1.49 KB
/
study_socket.py
File metadata and controls
58 lines (47 loc) · 1.49 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 17-4-19 上午10:58
# @Author : Tom.Lee
# @Description :
# @File : socket.py
# @Product : PyCharm
import socket
import threading
import time
class Server(object):
NUMBER = 0
def __init__(self, ip, port, message='hello'):
self.__ip = ip
self.__port = port
self.__message = message
def start(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((self.__ip, self.__port))
s.listen(0)
def run(**kwargs):
num = self.NUMBER
client = kwargs.get('client')
print self.NUMBER, client, client.recv(1024)
time.sleep(10)
client.sendall('%s:%d' % (self.__message, num))
client.close()
while True:
client, addr = s.accept()
if client:
self.NUMBER += 1
threading.Thread(target=run, name='T%d' % self.NUMBER,
kwargs={'client': client}).start()
class Client(object):
def __init__(self, ip, port, message='hi'):
self.__ip = ip
self.__port = port
self.__message = message
def start(self):
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c.connect((self.__ip, self.__port))
c.send(self.__message)
print c.recv(1024)
c.close()
if __name__ == '__main__':
server = Server('127.0.0.1', 1000)
server.start()