2

I have completed my web socket API in Python:

#import socket module
from socket import *

serverPort = 80 #Port Used
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('', serverPort))
serverSocket.listen(1)
print "The server is ready to receive"

while True:
    #Establish the connection
    print 'Ready to serve...'
    connectionSocket, addr = serverSocket.accept()   

    try:
        message = connectionSocket.recv(1024)
        filename = message.split()[1]
        f = open(filename[1:])
        outputdata = f.read()
        print outputdata    
        connectionSocket.send('\nHTTP OK\n')
        connectionSocket.send(outputdata)    

        for i in range(0, len(outputdata)):
            connectionSocket.send(outputdata[i])
        connectionSocket.close()   

    except IOError:
        connectionSocket.send('\n404 Not Found\n')
        connectionSocket.send('\n404 Not Found\n')

And I have a simple .html file which is on my XAMPP server. The task is to run the Python API and then access the host (in my case localhost:80) in order to access the .html file and get from the console all the necessary information.

Here is the problem: After I try to run my API it gives me the following error:

Traceback (most recent call last):
File "hw2.py", line 6, in <module>
serverSocket.bind(('', serverPort))
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use
  • Could it be that you have another webserver running at you computer already (Apache or something else) and that server holds localhost:80? Try to serve at another port, such as 8080. – JohanL Feb 08 '18 at 19:46
  • I used /etc/init.d/apache2 stop command before I restarted my XAMPP server. I also tried different port numbers but I'm still encountering the same issue. – Peter Holoubek Feb 08 '18 at 19:50
  • If you pick a random port number, does it work the first time, and then fail like this if you quit and restart the application? – Kevin Feb 09 '18 at 04:21
  • Also, please note that the code you present is not a Python API as much as a (very simple) webserver in its own right. I don't see how this is interacting with XAMPP (except possibly by trying to bind to the same port, which is bad). – JohanL Feb 09 '18 at 07:11
  • If I pick a random port number it gives me the same error right from the beginning. – Peter Holoubek Feb 10 '18 at 16:06
  • JohanL, I'm only following instructions which state clearly to use the same port number. But if you could elaborate on how you would approach it, I would love to learn. Thank you in advance ! – Peter Holoubek Feb 10 '18 at 16:07

0 Answers0