I am attempting to write a very simple server in python.
import socket
import sys
# Create a TCP/IP socket to listen on
server = socket.socket(socket.SOL_SOCKET, socket.SOCK_STREAM)
# Prevent from 'address already in use' upon server restart
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Bind the socket to port 8081 on all interfaces
server_address = ('localhost', 8081)
print 'starting up on %s port %s' % server_address
server.bind(server_address)
I have read what I think to be correct documentation for the socket library, and it suggests that the server.bind() takes an argument of a tuple. However, I get this error:
starting up on localhost port 8081
Traceback (most recent call last):
File "pyserver.py", line 14, in <module>
server.bind(server_address)
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
TypeError: argument must be string or read-only character buffer, not tuple
I have changed the argument to only a string, as the error warning suggests, and I get a
[Errno 98] Address already in use
error. I thought that the 8th line was in place to prevent that. What is going on?