forked from Gothamv/SocketChat.Py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
executable file
·145 lines (114 loc) · 4.1 KB
/
client.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
print('Enter 1 - Text Message\n\t2 - Image\n\t3 - Video Calling')
op = int(input())
if(op == 1):
"""Script for client. GUI using Tkinter."""
from socket import AF_INET, socket, SOCK_STREAM
from threading import Thread
import tkinter
def receive():
"""Handles incoming messages"""
while True:
try:
msg = client_socket.recv(BUFSIZE).decode("utf8")
msg_list.insert(tkinter.END, msg)
except OSError: # Incase the user has left the chat
break
def send(event=None): # Event is passed by binders
msg = my_msg.get()
my_msg.set("") # Clears the input field
client_socket.send(bytes(msg, "utf8"))
if msg == "{quit}":
client_socket.close()
top.quit()
def onClosingWindow(event=None):
"""This function is to called when the window is closed"""
my_msg.set("{quit}")
send()
top = tkinter.Tk()
top.title("SocketChat")
messages_frame = tkinter.Frame(top)
my_msg = tkinter.StringVar() # for messages to be sent
my_msg.set("Type your message here...")
scrollbar = tkinter.Scrollbar(messages_frame) # to navigate through the messages
msg_list = tkinter.Listbox(messages_frame, height=15, width=50, yscrollcommand=scrollbar.set)
scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
msg_list.pack(side=tkinter.LEFT, fill=tkinter.BOTH)
msg_list.pack()
messages_frame.pack()
entry_field = tkinter.Entry(top, textvariable=my_msg)
entry_field.bind("<Return>", send)
entry_field.pack()
send_button = tkinter.Button(top, text="Send", command=send)
send_button.pack()
top.protocol("WM_DELETE_WINDOW", onClosingWindow)
HOST = input('Enter host: ')
PORT = input('Enter port: ')
if not PORT:
PORT = 33000 # Default value.
else:
PORT = int(PORT)
BUFSIZE = 1024
ADDR = (HOST, PORT)
client_socket = socket(AF_INET, SOCK_STREAM)
client_socket.connect(ADDR)
receive_thread = Thread(target=receive)
receive_thread.start()
tkinter.mainloop() # Starts GUI execution.
if(op == 2):
import random
import socket, select
from time import gmtime, strftime
from random import randint
image = raw_input('Enter the Image name: ')
image = image + ".jpg"
HOST = '172.20.10.10'
PORT = 6666
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = (HOST, PORT)
sock.connect(server_address)
try:
# open image
myfile = open(image, 'rb')
bytes = myfile.read()
size = len(bytes)
# send image size to server
sock.sendall("SIZE %s" % size)
answer = sock.recv(4096)
print('answer = %s' % answer)
# send image to server
if answer == 'GOT SIZE':
sock.sendall(bytes)
# check what server send
answer = sock.recv(4096)
print('answer = %s' % answer)
if answer == 'GOT IMAGE' :
sock.sendall("BYE BYE ")
print('Image successfully send to server')
myfile.close()
finally:
sock.close()
if(op == 3):
import socket, videosocket
import StringIO
from videofeed import VideoFeed
import sys
class Client:
def __init__(self, ip_addr = "172.20.10.10"):
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.client_socket.connect((ip_addr, 6000))
self.vsock = videosocket.videosocket (self.client_socket)
self.videofeed = VideoFeed(1,"client",1)
self.data = StringIO.StringIO()
def connect(self):
while True:
frame=self.videofeed.get_frame()
self.vsock.vsend(frame)
frame = self.vsock.vreceive()
self.videofeed.set_frame(frame)
if __name__ == "__main__":
ip_addr = "172.20.10.10"
if len(sys.argv) == 2:
ip_addr = sys.argv[1]
print("Connecting to " + ip_addr + "....")
client = Client(ip_addr)
client.connect()