Skip to content

Instantly share code, notes, and snippets.

@beta
Created April 12, 2015 05:56
Show Gist options
  • Save beta/4bb61ff5bbcce308b7c0 to your computer and use it in GitHub Desktop.
Save beta/4bb61ff5bbcce308b7c0 to your computer and use it in GitHub Desktop.
Homework of my Python lesson, a half-duplex server-client chatting program
#!/usr/bin/env python
import socket
import time
BUFFSIZE = 1024
NS_HOST = 'localhost'
NS_PORT = 53333
S_HOST = ''
S_PORT = 0
print 'Connecting to name server...'
socketToNS = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socketToNS.connect((NS_HOST, NS_PORT))
print 'Connected to name server.'
# tell the name server this is a server
data = 'client'
socketToNS.sendall(data)
# receive a server's address and port
data = socketToNS.recv(BUFFSIZE)
if cmp(data, 'no') == 0:
print 'No server running.'
exit()
else:
args = data.split('&')
S_HOST = args[0]
S_PORT = int(args[1])
socketToNS.close()
socketToServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socketToServer.connect((S_HOST, S_PORT))
print 'Connected'
print '(type "exit" to exit)'
f = open('chat_with_server_' + S_HOST + '_' + str(S_PORT) + '.txt', 'a')
while True:
while True:
data = raw_input('> ')
if data:
break
socketToServer.sendall(data)
record = 'Client@%s: %s' % (time.strftime('%H:%M:%S'), data)
print record
f.write(record + '\n')
if cmp(data, 'exit') == 0:
break;
data = socketToServer.recv(BUFFSIZE)
record = 'Server@%s: %s' % (time.strftime('%H:%M:%S'), data)
print record
f.write(record + '\n')
if cmp(data, 'exit') == 0:
break;
f.close()
socketToServer.close()
#!/usr/bin/env python
import socket
BUFFSIZE = 1024
HOST = 'localhost'
PORT = 53333
servers = []
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
while True:
print 'Waiting for connection...'
s.listen(1)
conn, addr = s.accept()
print 'Connection from', addr
# get the type
data = conn.recv(BUFFSIZE)
if cmp(data, 'server') == 0:
# a server
print 'New server connected.'
data = conn.recv(BUFFSIZE)
servers.append(data)
else:
# a client
print 'New client connected.'
if len(servers) == 0:
print 'But no server is running.'
data = 'no'
conn.sendall(data)
else:
data = servers.pop(0)
conn.sendall(data)
print 'Assigned server', data, 'to client.'
conn.close()
s.close()
#!/usr/bin/env python
import socket
import time
BUFFSIZE = 1024
HOST = ''
PORT = 0
NS_HOST = 'localhost'
NS_PORT = 53333
print 'Starting server...'
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
S_HOST, S_PORT = s.getsockname()
print 'Connecting to name server...'
socketToNS = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socketToNS.connect((NS_HOST, NS_PORT))
print 'Connected to name server.'
# tell the name server this is a server
data = 'server'
socketToNS.sendall(data)
# send this server's address and port
data = socket.gethostbyname(socket.gethostname()) + '&' + str(S_PORT)
socketToNS.sendall(data)
socketToNS.close()
print 'Address and port sent to name server.'
print 'Waiting for client...'
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
print '(type "exit" to exit)'
f = open('chat_with_client_' + addr[0] + '_' + str(addr[1]) + '.txt', 'a')
while True:
data = conn.recv(BUFFSIZE)
record = 'Client@%s: %s' % (time.strftime('%H:%M:%S'), data)
print record
f.write(record + '\n')
if cmp(data, 'exit') == 0:
break;
while True:
data = raw_input('> ')
if data:
break
conn.sendall(data)
record = 'Server@%s: %s' % (time.strftime('%H:%M:%S'), data)
print record
f.write(record + '\n')
if cmp(data, 'exit') == 0:
break;
f.close()
conn.close()
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment