Jim's
Tutorials

Spring 2018
course
site

April 12

we talked about client - server programming and processes

"""
 server.py

 example of a server

"""

# Following recipe at https://docs.python.org/2/howto/sockets.html

from socket import socket, AF_INET, SOCK_STREAM, gethostname
import random

port = 12345

#create an INET, STREAMing socket
serversocket = socket(AF_INET, SOCK_STREAM)
#bind the socket to a public host,
# and a well-known port
serversocket.bind(('', port))
#become a server socket
serversocket.listen(1)

print "waiting for connection ..."
(clientsocket, address) = serversocket.accept()

print "connection: address = {}, clientsocket = {}"
clientsocket.send("Hi there! This is the server!!!")

number = random.randint(1,6)

clientsocket.send("Hi there! This is the server!!!")
for i in range(6): # max of 6 guesses
    clientsocket.send("Guess {}: ".format(i))
    data = clientsocket.rcv(10)  # what is this 10? bytes?
    print "He said {}".format(data)

in one terminal:

softmaple:Desktop mahoney$ python2 server.py 
waiting for connection ...
connection: address = {}, clientsocket = {}
Traceback (most recent call last):
  File "server.py", line 34, in <module>
    data = clientsocket.rcv(10)  # what is this 10? bytes?
AttributeError: '_socketobject' object has no attribute 'rcv'
softmaple:Desktop mahoney$ 

while in another terminal:

Last login: Thu Apr 12 09:53:09 on ttys004
softmaple:~ mahoney$ telnet localhost 12345
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Hi there! This is the server!!!Hi there! This is the server!!!Guess 0: Connection closed by foreign host.
softmaple:~ mahoney$ 


https://cs.marlboro.college /cours /spring2018 /jims_tutorials /web_tech /apr12
last modified Thu April 25 2024 5:22 pm