Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
When i try to create a webserver through a port throwing error

When i try to create a webserver through a port and for second time if i try to create to same port it is throwing following error message.

[Errno 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted+python windows
I have tried 2 code snippets like below Please correct me where i am going wrong:
Snippet 1:
import SimpleHTTPServer
import SocketServer
import os
def createwebserver(streampath):
PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT
Snippet 2:
import threading

import os
import signal
 from http.server import HTTPServer, BaseHTTPRequestHandler # Python 3
except ImportError:
 import SimpleHTTPServer
 from BaseHTTPServer import HTTPServer # Python 2
 from SimpleHTTPServer import SimpleHTTPRequestHandler as BaseHTTPRequestHandler
server = HTTPServer(('localhost', 8000), BaseHTTPRequestHandler)
thread = threading.Thread(target = server.serve_forever)
thread.deamon = True
def up(StreamPath):
 print('starting server on port {}'.format(server.server_port))
def down(portnumber):
 server = HTTPServer(('localhost', portnumber), BaseHTTPRequestHandler)

 print('stopping server on port {}'.format(server.server_port))

if __name__ == '__main__':
Note: I have also so many different ways but i don't know where i am going wrong. I also seen articles related to SO_REUSEADDR but no use. Please help ASAP.
Only one program can use a port at once. That's just how operating systems work.

What would you expect to happen if they both were bound to the port? That they'd both get to handle all requests? And a client that requested something would get two responses somehow?
It's dangerous to go alone!  Take this: https://docs.python.org/3/library/index.html
HackerRank seems neat: https://www.hackerrank.com/nilamo
No I mean i tried both the codes each one at a time but am able to create webserver on the port given but i am not able to shutdown the server. I used these commands server.server_close()
server.shutdown(). And also after closing the webserver how to end the process permanently on the port background because again i am not able to create the webserver to the same port i am getting exception.

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)