When i try to create a webserver through a port throwing error - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: When i try to create a webserver through a port throwing error (/thread-1214.html) |
When i try to create a webserver through a port throwing error - pras120687 - Dec-13-2016 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. RE: When i try to create a webserver through a port throwing error - nilamo - Dec-13-2016 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? RE: When i try to create a webserver through a port throwing error - pras120687 - Dec-14-2016 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. |