Skip to content

Instantly share code, notes, and snippets.

@tuxmartin
Last active April 11, 2020 20:38
Show Gist options
  • Save tuxmartin/e7c85f84153ba15576c5 to your computer and use it in GitHub Desktop.
Save tuxmartin/e7c85f84153ba15576c5 to your computer and use it in GitHub Desktop.
Python socket - server and client example (push notifications)
import socket
host = 'localhost'
port = 1234
buf = 1024
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect((host, port))
print "Sending 'test1\\n'"
clientsocket.send('test1\n')
print "REPLY: " + clientsocket.recv(buf)
print "Sending 'test2'"
clientsocket.send('test2')
print "REPLY: " + clientsocket.recv(buf)
print "Sending 'abc'"
clientsocket.send('abc')
print "REPLY: " + clientsocket.recv(buf)
print "Sending 'abc'"
clientsocket.send('abc')
print "REPLY: " + clientsocket.recv(buf)
path = raw_input('JPG file [/tmp/fotka.jpg]:') or '/tmp/fotka.jpg'
clientsocket.send('jpg_' + path)
reply = clientsocket.recv(buf)
print "___Binary data START___"
print reply
print "___Binary data END___"
file = open('fotka.jpg', 'w')
file.write(reply)
file.close()
print "New file 'fotka.jpg' was created."
print "Sending 'bye'"
clientsocket.send('bye\n')
print "REPLY: " + clientsocket.recv(buf)
clientsocket.close()
from socket import socket, AF_INET, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR
import thread
import time
import datetime
host = '0.0.0.0'
port = 1234
buf = 1024
addr = (host, port)
serversocket = socket(AF_INET, SOCK_STREAM)
serversocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
serversocket.bind(addr)
serversocket.listen(10)
clients = [serversocket]
def handler(clientsocket, clientaddr):
print "Accepted connection from: ", clientaddr
while True:
data = clientsocket.recv(1024)
print "_" + data + "_"
if data == "bye\n" or not data:
break
elif data.startswith("jpg_"): # jpg_/tmp/fotka.jpg
file = data.split('_')[1].strip()
try:
bytes = open(file).read()
print len(bytes)
clientsocket.send(bytes)
except IOError:
print "File '" + file + "' not found!"
clientsocket.send("FILE_NOT_FOUND\n")
elif data == "test1\n":
clientsocket.send("test1\n")
elif "test2" in data: # Nemusi byt EOL, radeji nepouzivat!
clientsocket.send("test2\n")
else:
clientsocket.send("ECHO: " + data + '\n')
clients.remove(clientsocket)
clientsocket.close()
def push():
while True:
for i in clients:
if i is not serversocket: # neposilat sam sobe
i.send("Curent date and time: " + str(datetime.datetime.now()) + '\n')
time.sleep(10) # [s]
thread.start_new_thread(push, ())
while True:
try:
print "Server is listening for connections\n"
clientsocket, clientaddr = serversocket.accept()
clients.append(clientsocket)
thread.start_new_thread(handler, (clientsocket, clientaddr))
except KeyboardInterrupt: # Ctrl+C # FIXME: vraci "raise error(EBADF, 'Bad file descriptor')"
print "Closing server socket..."
serversocket.close()
@purvalpatel
Copy link

it works

@evertonmatos
Copy link

Put a "break" after the error: Ctrl+C # FIXME: vraci "raise error(EBADF, 'Bad file descriptor')"
For me worked well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment