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
#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.
Quote
#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?
It's dangerous to go alone!  Take this: https://docs.python.org/3/library/index.html
Quote
#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.
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)