Python socket giving connection refused only on browser -
as know app 100% not work, test, expecting other error other connection refused on browser, don't log browser tried connect! made me think before making work need see why gets refused(i'm doing experiment http!) note: use socket not http libraries, , problem exists on browser , not client app wrote other answers won't me code:
#!/usr/bin/python # server.py file import socket # import socket module s = socket.socket() # create socket object host = socket.gethostname() # local machine name port = 1234 # reserve port service. s.bind((host, port)) # bind port s.listen(5) # wait client connection. while true: c, addr = s.accept() # establish connection client. print 'got connection from', addr c.send(open("gpioweb/index.html").read()) print c.recv(1024) c.close() # close connection
client :
#!/usr/bin/python # client.py file import socket # import socket module s = socket.socket() # create socket object host = socket.gethostname() # local machine name port = 1234 # reserve port service. s.connect((host, port)) print s.recv(1024) s.close # close socket when done
you can access server.py program through browser, need tell browser (so can tell os) how expect there.
when do
host = socket.gethostname() # local machine name s.bind((host, 1234)) # bind port
you're binding specific interface.
add print(host)
in there see interface you're binding to. then, in browser, enter <host>:1234
address -- <host>
printed.
your browser display contents of gpioweb/index.html
, server.py program display like:
got connection ('127.0.0.1', 63358) / 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, gecko) chrome/41.0.2272.89 safari/537.36 accept-encoding: gzip, deflate, sdch accept-language: en-us,en;q=0.8
alternatively, bind all available interfaces with,
port = 1234 # reserve port service. s.bind(('', port)) # bind port
(note ''
used host)
now, should able communicate program through sorts of addresses, example:
localhost:1234 127.0.0.1:1234 <your lan ip>:1234
some of these may depend on firewall settings, may consider disabling temporarily if you're not getting results expect, updating configuration appropriately.
Comments
Post a Comment