Skip to content

Instantly share code, notes, and snippets.

@wwj718
Forked from marvin/client.py
Created March 24, 2017 11:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wwj718/17339a2455d5f3c923ee6da7db2c5702 to your computer and use it in GitHub Desktop.
Save wwj718/17339a2455d5f3c923ee6da7db2c5702 to your computer and use it in GitHub Desktop.
simple python client/server socket binary stream
import socket
HOST = 'localhost'
PORT = 9876
ADDR = (HOST,PORT)
BUFSIZE = 4096
videofile = "videos/royalty-free_footage_wien_18_640x360.mp4"
bytes = open(videofile).read()
print len(bytes)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
client.send(bytes)
client.close()
import socket
HOST = ''
PORT = 9876
ADDR = (HOST,PORT)
BUFSIZE = 4096
serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serv.bind(ADDR)
serv.listen(5)
print 'listening ...'
while True:
conn, addr = serv.accept()
print 'client connected ... ', addr
myfile = open('testfile.mov', 'w')
while True:
data = conn.recv(BUFSIZE)
if not data: break
myfile.write(data)
print 'writing file ....'
myfile.close()
print 'finished writing file'
conn.close()
print 'client disconnected'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment