-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver_with_thread.py
More file actions
30 lines (24 loc) · 1.14 KB
/
server_with_thread.py
File metadata and controls
30 lines (24 loc) · 1.14 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
import socket
from threading import *
#!/usr/bin/python # This is server.py file
import socket # Import socket module
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
class client(Thread):
def __init__(self, socket, address):
Thread.__init__(self)
self.sock = socket
self.addr = address
self.start()
def run(self):
while True:
self.sock.send('Thank you for connecting')
s.listen(5) # Now wait for client connection.
print ('server started and listening')
while True:
clientsocket, address = s.accept() # Establish connection with client.
print 'Got connection from', address
client(clientsocket, address)
#client.close() # Close the connection