Amy Gamble
Amy Gamble

Reputation: 175

Python socket giving connection refused only on browser

As i know this app will 100% not work, but this is just for test, but i was expecting some other error other than connection refused on my browser, i don't even get the log that my browser tried to connect! so it made me think before making it work i need to see why it gets refused(I'm doing this to experiment about http!) Note: i use socket not any HTTP libraries, and this problem only exists on browser and not the client app i wrote so other answers won't help me this is my code:

#!/usr/bin/python           # This is server.py file

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 1234                # Reserve a port for your service.
s.bind((host, port))        # Bind to the port

s.listen(5)                 # Now wait for client connection.
while True:
   c, addr = s.accept()     # Establish connection with client.
   print 'Got connection from', addr
   c.send(open("GPIOWEB/index.html").read())
   print c.recv(1024)
   c.close()                # Close the connection

client :

#!/usr/bin/python           # This is client.py file

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 1234                # Reserve a port for your service.

s.connect((host, port))
print s.recv(1024)
s.close                     # Close the socket when done

Upvotes: 2

Views: 2075

Answers (2)

jedwards
jedwards

Reputation: 30240

You can access your server.py program through a browser, but you need to tell the browser (so it can tell the OS) how you expect to get there.

When you do

host = socket.gethostname() # Get local machine name
s.bind((host, 1234))        # Bind to the port

You're binding to a specific interface.

Add a print(host) in there to see what interface you're binding to. Then, in your browser, enter <host>:1234 for the address -- where <host> is what was printed.

Your browser will display the contents of GPIOWEB/index.html and your server.py program will display something like:

Got connection from ('127.0.0.1', 63358)
GET / HTTP/1.1
Host: localhost:1234
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

Alternatively, bind to all available interfaces with,

port = 1234                 # Reserve a port for your service.
s.bind(('', port))          # Bind to the port

(Note the '' used for host)

Now, you should be able to communicate with the program through all sorts of addresses, for example:

localhost:1234
127.0.0.1:1234
<Your LAN IP>:1234

Some of these may depend on your firewall settings, you may consider disabling it temporarily if you're not getting the results you expect, then updating its configuration appropriately.

Upvotes: 4

Jose Raul Barreras
Jose Raul Barreras

Reputation: 859

You implemented a protocol in your app:

on_connect SERVER send index.html to CLIENT

Your client speaks this protocol. The browser did not. Use a sniffer (wireshark, tcpdump, etc...) to see the server-client traffic.

Upvotes: 0

Related Questions