views:

327

answers:

6

I just found out that I can write a really simple web server using Python. I have already an Apache web server I would like to try the Python based web server on this machine. But I am afraid that I can get some kind of conflict if I try it. I mean how two web server will "decide" who needs to server a request from a client?

+18  A: 

Make them listen to different ports and you will be fine.

The default web port is 80. When you open some url in browser without specifying a port, 80 is used by default.

You can configure your web server to listen to a different port but then you will also need to specify it explicitly in the url:

http://localhost:8080

When choosing a port pay attention that this particular port number is not yet in use by any software you have installed and running on your box. Otherwise, as you correctly guessed, there will be a conflict.

P.S. Just a few days ago doing reinstallation I got IIS not able to start (seemingly without reason). Turned out the new version of Skype occupied this default port! Had to remove its setting "Use port 80 and 443 as alternatives for incoming connections".

Developer Art
OK. Is there a default port used by web browsers? Can I specify the port when I try to open a web page on a specific web server?
Roman
The default HTTP port is 80. You can specify ports in the URL like so: `http://yourserver:portnumber`.
Mark B
The default is 80 for HTTP 443 for HTTPS. You can specify a different one using :
Ofir
A: 

You cannot open two web servers in the same port (which default is 80), if you desire to make two or more web servers, you have to use different ports.

If you are using a DNS, you could easily set up your web server to respond with different web sites to different requests, that could be useful if you need to have different web sites for subdomains or different domains.

jpabluz
HTTP/1.1 for the win
Matt Joiner
indeed, hail HOST-HEADER!
Dal
+5  A: 

A web server is tied to a specific port. Normally, this is port 80. If the port is not explicitly specified, this is the port that a browser will attempt to hit.

You can get your alternate server to run on a different port ( 8080 or 8081 seem to be popular alts for web servers, but the choice is yours ).

This'll stop the conflict from happening. Everything going to port 80 hits your old server. Everything going to 8080 ( or whatever port you decide to run your server on ) will hit your simple python server.

To hit your other server, use a URL like :-

http://localhost:8080/

Paul Alan Taylor
A: 

The webservers would have no say in who services a connection request (this task is still at the operating system level). Furthermore, without special socket options, the socket must be bound to a unique combination of interface, internet address and port.

Matt Joiner
A: 

I would suggest you dedicate one server to serving https (port 443) requests.

That way you can avoid the port conflict others have mentioned while also not requiring users to type anything strange into their browsers (arbitrary port numbers). You can even have each server redirect traffic to the other (e.g. the http server recieves an http request for a host name it knows the https server handles so it can redirect the request to https witht the same host name, thereby transferring the request to the appropriate server).

Server A:

http://localhost

Server B:

https://localhost
Dolph
A: 

If you actually want to run separate servers to test out server software see the other answers, but...

It sounds like (because you are a developer, not a sysadmin right?) you really just want to run Python and PHP sites on the same computer. So, forgive me if I'm reading into your question, but this setup allows me to run both kinds of sites on the same computer with the same port (port 80) in one server, Apache.

I make new entries in my /etc/hosts file (or C:\Windows\System32\drivers\etc\hosts on Windows) and point them to 127.0.0.1:

127.0.0.1      localhost

# development projects
127.0.0.1      somephpsite.com.local
127.0.0.1      www.somephpsite.com.local
127.0.0.1      otherpythonsite.com.local
127.0.0.1      www.otherpythonsite.com.local

Then in Apache I add VirtualHosts for each site:

<VirtualHost *:80>
    DocumentRoot "/Library/WebServer/Documents"
    ServerName localhost
</VirtualHost>

<VirtualHost *:80>
    <Directory "/Users/Robert/Projects/SomeSite/somephpsite.com">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
    DocumentRoot "/Users/Robert/Projects/SomeSite/somephpsite.com"
    ServerName somephpsite.com.local
    ServerAlias www.somephpsite.com.local
    ErrorLog "/Users/Robert/Projects/SomeSite/error.log"
    CustomLog "/Users/Robert/Projects/SomeSite/access.log" common
</VirtualHost>

<VirtualHost *:80>
    <Directory "/Users/Robert/Projects/OtherSite/otherpythonsite.com">
        Order allow,deny
        Allow from all
    </Directory>
    DocumentRoot "/Users/Robert/Projects/OtherSite/otherpythonsite.com/static"
    Alias /(.*(\.css|\.gif|\.ico|\.jpg|\.js|\.pdf|\.txt)) /Users/Robert/Projects/OtherSite/otherpythonsite.com/static/$1
    WSGIScriptAlias / /Users/Robert/Projects/OtherSite/otherpythonsite.com/wsgi.py
    ServerName otherpythonsite.com.local
    ServerAlias www.otherpythonsite.com.local
    ErrorLog "/Users/Robert/Projects/OtherSite/error.log"
    CustomLog "/Users/Robert/Projects/OtherSite/access.log" common
</VirtualHost>

So, the PHP sites run in the DocumentRoot like they always do. And the Python sites run in WSGI. And they both run in Apache. Then to test, I just add ".local" in whatever browser I'm using to work on my local copy.

Robert