-
Notifications
You must be signed in to change notification settings - Fork 23
/
Connection.py
60 lines (51 loc) · 1.62 KB
/
Connection.py
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
59
60
import sys
import socket
class socket_connection:
def connect_to_server(self,host,port):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host,port))
return sock
except:
print "Unexpected error:", sys.exc_info()[0]
print "Error in Connecting"
def start_server(self,host,port):
server_sock = socket.socket() # Create a socket object
server_sock.bind((host, port)) # use host if u know about the IP address or manually give the IP address
print 'Server Started'
server_sock.listen(5) # Waiting for client
socket_connection.c, addr = server_sock.accept() # Establish connection with client.
print 'Got connection from', addr
return socket_connection.c
def read_data(self,sock):
size = 2
try:
data = sock.recv(size)
#print data
return data
except :
print "Error in Recieving"
def send_data(self,sock):
# one way
dat = raw_input('Enter Data : ')
try:
sock.send(dat)
print dat
except :
print "Error in Sending"
def __init__(self):
print "connect_to_server(host,port)"
print "start_server(host,port)"
'''
try:
s = socket_connection()
soc = s.start_server('192.168.43.96',12346)
while True:
re = s.read_data(soc)
print re
if re == '0_0':
break
except KeyboardInterrupt:
print 'broke'
soc.close
'''