Last active
August 29, 2015 14:24
-
-
Save iegik/2b89decc045e2f42dddc to your computer and use it in GitHub Desktop.
Socket server in python using select function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| ''' | |
| Simple socket server using threads | |
| http://tools.ietf.org/html/rfc6455 | |
| ''' | |
| import socket | |
| import sys | |
| HOST = '' # Symbolic name, meaning all available interfaces | |
| PORT = 8888 # Arbitrary non-privileged port | |
| RECV_BUFFER = 4096 | |
| s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| print 'Socket created' | |
| #Bind socket to local host and port | |
| try: | |
| s.bind((HOST, PORT)) | |
| except socket.error as msg: | |
| print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] | |
| sys.exit() | |
| print 'Socket bind complete' | |
| #Start listening on socket | |
| s.listen(10) | |
| print 'Socket now listening' | |
| #now keep talking with the client | |
| while 1: | |
| #wait to accept a connection - blocking call | |
| conn, addr = s.accept() | |
| print 'Connected with ' + addr[0] + ':' + str(addr[1]) | |
| data = s.recv(RECV_BUFFER) | |
| if data: | |
| s.send(str.encode('OK ... ' + bytes.decode(data))) | |
| s.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #/bin/sh | |
| exec 3<>/dev/tcp/127.0.0.1/8888 | |
| echo -e "GET / HTTP/1.1\n\n" >&3 | |
| cat <&3 | |
| #tail -f ~/ucm.out >&3 | |
| #cat <&3 | |
| echo -e $@ >&3 | |
| exit $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment