[Tutor] Questions Regarding Sockets

Khalid Al-Ghamdi emailkgnow at gmail.com
Sat Apr 14 18:41:43 CEST 2012


Hi All,

(python 3.2 on windows)

I have a couple of questions regarding the below code:

1- In line (15), what are these variables tcpCliSock, addr supposed to hold
and do?
2- Why do I have to specify the buffer size and what does it mean?
3- When I try to run the below code and its corresponding client it works
ok for the first time, but then it gives me this error:

Traceback (most recent call last):
  File "C:\Python32\Khalid Stuff\tsTserv3.py", line 12, in <module>
    tcpSerSock.bind(ADDR)
socket.error: [Errno 10048] Only one usage of each socket address
(protocol/network address/port) is normally permitted

I thought it had to do with  the address so I changed the port and it
worked ok. so,:

A/ isn't the optional tcpSerSock.close() supposed to close the connection
for later reuse?
B/ why is it when i go to the IDLE and enter tcpSerSock.close() and it
accepts it, it still gives the same error and doesn't close the connection
for reuse by my code?

Thanks a lot


   1. from socket import *
   2. from time import ctime
   3.
   4. HOST = ''
   5.  PORT = 21567
   6.  BUFSIZ = 1024
   7.  ADDR =(HOST, PORT)
   8.
   9. tcpSerSock = socket(AF_INET, SOCK_STREAM)
   10. tcpSerSock.bind(ADDR)
   11.  tcpSerSock.listen(5)
   12.
   13. while True:
   14.      print('waiting for connection ...')
   15.     tcpCliSock, addr = tcpSerSock.accept()
   16.     print('...connected from: ', addr)
   17.
   18.     while True:
   19.          data = tcpCliSock.recv(BUFSIZ)
   20.          if not data:
   21.              break
   22.         tcpCliSock.send(bytes('[{}] {}'.format(ctime(),data.decode(
   'utf-8')),'utf-8'))
   23.
   24.     tcpCliSock.close()
   25.  tcpSerSock.close()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120414/6311c2b7/attachment-0001.html>


More information about the Tutor mailing list