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

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):
os.chdir(streampath)
PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

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

print "serving at port", PORT
httpd.serve_forever()
Snippet 2:
import threading

import os
import signal
try:
 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):
 os.chdir(StreamPath)
 thread.start()
 print('starting server on port {}'.format(server.server_port))
def down(portnumber):
 print('test')
 print('test1')
 #server.shutdown()
 server = HTTPServer(('localhost', portnumber), BaseHTTPRequestHandler)
 server.server_close()
 server.shutdown()

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

if __name__ == '__main__':
   #up("C:\\\\QED")
   down(8000)
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.
Reply
#2
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?
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  combining flask webserver and another applicaiton maciejMatthias 3 2,228 May-10-2020, 11:19 PM
Last Post: snippsat
  Handle parameters in POST request for python webserver? JBristow1729 1 7,658 Jul-16-2019, 10:59 PM
Last Post: scidam
  Why my python code is throwing several errors? amanulla1601 2 2,516 Aug-29-2018, 02:05 PM
Last Post: sumandas89

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020