Skip to content

Instantly share code, notes, and snippets.

@talhaHavadar
Created September 10, 2016 22:47
Show Gist options
  • Save talhaHavadar/0bb0e64ed44a0ef88c29ba98d5709097 to your computer and use it in GitHub Desktop.
Save talhaHavadar/0bb0e64ed44a0ef88c29ba98d5709097 to your computer and use it in GitHub Desktop.
import socket, io, struct, time
s = socket.socket() # defaults family AF_INET and type SOCK_STREAM (tcp)
HOST = ""# "192.168.1.39" # computer's ip address
PORT = 51215 # port number that computer will listen
s.connect((HOST,PORT))
try:
with open('./send_data/profile.jpg', 'rb') as file:
data = file.read(1024)
while data:
s.send(data)
data = file.read(1024)
print "File was sent."
finally:
s.close()
print "Done."
import socketserver
import numpy as np
import cv2
class FileTCPHandler(socketserver.StreamRequestHandler):
"""
The request handler class for our server.
It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
"""
def setup(self):
super(FileTCPHandler, self).setup()
self.received_data_file = open("received_data_handler.txt", 'wb')
print("File was opened")
def handle(self):
self.data = ''
self.data = self.rfile.read(1024)
while self.data:
first_i = self.data.decode('cp437').find('FILE_START')
last_i = self.data.decode('cp437').find('FILE_END')
if first_i != -1 and last_i != -1:
f_content = self.data[first_i + len('FILE_START') : last_i]
self.data = self.data[last_i + len('FILE_END') : ]
image = cv2.imdecode(np.fromstring(f_content, dtype=np.uint8), flags = 1)
cv2.imshow('image', image)
cv2.waitKey(1)
self.data += self.rfile.read(1024)
cv2.destroyAllWindows()
def finish(self):
super(FileTCPHandler, self).finish()
self.received_data_file.close()
print("File was closed.")
server = socketserver.TCPServer(('', 51215), FileTCPHandler)
server.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment