forked from NULLify-UNO/Scripting-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.py
More file actions
executable file
·38 lines (31 loc) · 1.03 KB
/
chat.py
File metadata and controls
executable file
·38 lines (31 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#! /usr/bin/python
from socket import * #import the socket library
#Simple chat client, not even close to done
#Allows the user to chat with one client
#Can only send one message at a time
#ADD THREADING
#Example is in thread.py
##let's set up some constants
HOST = '' #we are the host
PORT = 56754 #arbitrary port not currently in use
ADDR = (HOST,PORT) #we need a tuple for the address
serv = socket( AF_INET,SOCK_STREAM)
##bind our socket to the address
serv.bind((ADDR)) #the double parens are to create a tuple with one element
serv.listen(5) #5 is the maximum number of queued connections we'll allow
print 'listening...'
conn,addr = serv.accept() #accept the connection
print '...connected!'
conn.send('Chat!!!\nEnter name: ')
cname = conn.recv(1024)
cname = cname.rstrip('\n')
print cname+' has connected'
print 'Say something'
while True:
message = raw_input('Message: ')
conn.send('server: '+message+'\n')
conn.send('message: ')
print cname+': '+conn.recv(1024)
if 'exit' in message:
conn.close()
break