Last active
August 29, 2015 13:56
-
-
Save SeavantUUz/8916681 to your computer and use it in GitHub Desktop.
fresh to network programming
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
import socket,select | |
CONNECTION_LIST = [] | |
RECV_BUFFER = 4096 | |
PORT = 5000 | |
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) | |
server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) | |
server_socket.bind(("0.0.0.0",PORT)) | |
server_socket.listen(10) | |
CONNECTION_LIST.append(server_socket) | |
print "Chat server started on port "+str(PORT) | |
while 1: | |
read_sockets,write_sockets,error_sockets = select.select(CONNECTION_LIST,[],[]) | |
for sock in read_sockets: | |
if sock == server_socket: | |
sockfd,addr = server_socket.accept() | |
CONNECTION_LIST.append(sockfd) | |
print "Client (%s,%s) connected" % addr | |
else: | |
try: | |
data = sock.recv(RECV_BUFFER) | |
if data: | |
print 'send' | |
sock.send("HTTP/1.1 200 OK\r\n\r\n") | |
sock.send("Hello Kochiya") | |
sock.close() | |
CONNECTION_LIST.remove(sock) | |
except: | |
#broadcast_data(sock,"Client (%s,%s) is offline" % addr) | |
print "Client (%s,%s) is offline" % addr | |
sock.close() | |
CONNECTION_LIST.remove(sock) | |
continue | |
server_socket.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment