-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket_server.py
More file actions
52 lines (40 loc) · 1.17 KB
/
socket_server.py
File metadata and controls
52 lines (40 loc) · 1.17 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
import socket
from threading import Thread
import time
TCP_IP = 'localhost'
TCP_PORT = 9001
BUFFER_SIZE = 1024
class ClientThread(Thread):
def __init__(self, ip, port, sock):
Thread.__init__(self)
self.ip = ip
self.port = port
self.sock = sock
print(" New thread started for "+ip+":"+str(port))
def run(self):
filename = '0.jpg'
f = open(filename, 'rb')
while True:
l = f.read(BUFFER_SIZE)
while (l):
self.sock.send(l)
#print('Sent ',repr(l))
l = f.read(BUFFER_SIZE)
if not l:
f.close()
self.sock.close()
break
tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpsock.bind((TCP_IP, TCP_PORT))
threads = []
while True:
tcpsock.listen(5)
print("Waiting for incoming connections...")
(conn, (ip, port)) = tcpsock.accept()
print('Got connection from ', (ip, port))
newthread = ClientThread(ip, port, conn)
newthread.start()
threads.append(newthread)
for t in threads:
t.join()