Skip to content

Instantly share code, notes, and snippets.

@kunst1080

kunst1080/server

Last active Aug 29, 2015
Embed
What would you like to do?
simple server
#!/usr/local/bin/python
import socket
import os
from subprocess import Popen, PIPE
PORT_NO = 8080
HOSTNAME = ''
SCRIPT = "listen"
def main():
# Server Socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind((HOSTNAME, PORT_NO))
server.listen(1)
print 'listenning...'
# Client Socket
connection = None
try:
next_script = os.path.abspath(os.path.dirname(__file__) + "/" + SCRIPT)
while True:
(connection, address) = server.accept()
data = connection.recv(1024)
if data:
# I/O with PIPE
process = Popen(next_script, stdin=PIPE, stdout=PIPE)
process.stdin.write(data)
process.stdin.close()
result = process.stdout.read()
ret = process.wait()
connection.send(result)
connection.close()
except socket.error, msg:
print msg
except KeyboardInterrupt:
print ''
finally:
print 'stop'
if not connection is None:
connection.close()
server.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.