Skip to content

Instantly share code, notes, and snippets.

@harrifeng
Last active December 30, 2015 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harrifeng/7802922 to your computer and use it in GitHub Desktop.
Save harrifeng/7802922 to your computer and use it in GitHub Desktop.
# Client Side----------------------------------------
# Please cheng the `sh-rd-hfeng to your server's host-name
# Usage: python client.py <str> -- if <str> is 'Done', it
# will tell the server you are OK now and the server will close,
# any other strings will continue the server works
import socket
import sys
HOST, PORT = "sh-rd-hfeng", 9988
data = " ".join(sys.argv[1:])
# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# Connect to server and send data
sock.connect((HOST, PORT))
sock.sendall(data + "\n")
# Receive data from the server and shut down
received = sock.recv(1024)
finally:
sock.close()
print "Sent: {0}".format(data)
print "Received: {0}".format(received)
# Server Side----------------------------------------
import socket
if __name__ == '__main__':
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('0.0.0.0', 9988))
sock.listen(5)
g_should_listen = True
while g_should_listen:
connection,address = sock.accept()
try:
connection.settimeout(5)
buf = connection.recv(1024)
if buf.strip().upper() == 'DONE':
connection.send('Thanks for your notification!')
g_should_listen = False
else:
connection.send('OK I will wait')
except socket.timeout:
print 'time out'
connection.close()
#------Improved Server
import socket
import subprocess
if __name__ == '__main__':
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('0.0.0.0', 9988))
sock.listen(5)
g_should_listen = True
while g_should_listen:
connection,address = sock.accept()
try:
connection.settimeout(5)
buf = connection.recv(1024)
if buf.strip().upper() == 'DONE':
p = subprocess.Popen(["/builds/penguin/labviewrt/Installers/linux/2013/13.0/hypervisor/Distribution/build_submit_all.sh"], stdout=subprocess.PIPE)
out, err = p.communicate()
connection.send('Following is the build details\n' + out)
# g_should_listen = False
else:
connection.send('OK I will wait')
except socket.timeout:
print 'time out'
connection.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment