-1

I opened port 8080 using python socket: Sk.bind(ip_addrr, 8080) But I want it to open a html page in that port,so that when I navigate to :8080 in browser I must get a web page.Any ideas?

my problem is that I need to get a html page which I created, to be displayed in port 8080.for example I have index.html for port 80 similarly,I need to have a html page in port 8080.How will I do that?

The Fool
  • 16,715
  • 5
  • 52
  • 86
Trapmap
  • 13
  • 2
  • It depends on which network interface you expose the port. The ip_adder variable is what you need to use technically. `ip_addr:port` If ip addr is 0.0.0.0 then it will be exposed on all network interfaces. So localhost works in that case. Anyways, likely you can try to go to `localhost:8080` in your browser. – The Fool Mar 24 '21 at 09:29
  • No, my problem is that I need to get a html page which I created, to be displayed in port 8080.for example I have index.html for port 80 similarly,I need to have a html page in port 8080.How will I do that? – Trapmap Mar 24 '21 at 13:26
  • Please [edit] your question and add more details. Copy&paste your code (a [mre]) to the question. Do you want your Python script to provide the web page? Then your script has to handle HTTP. There might be libraries available, or you can implement a minimalistic HTTP yourself. – Bodo Mar 24 '21 at 15:12

1 Answers1

0

Very minimal example I was able to put together after some research. You can find this also on https://replit.com/@bluebrown/python-socket-html

import socket

s = socket.socket()
s.bind(('0.0.0.0', 8080))
s.listen(1)

with open('index.html', 'rb') as file:
  html = file.read()
  while True:    
    conn, addr = s.accept()
    with conn:
      print('Connected by', addr)
      req = conn.recv(1024)
      print('request:', req)
      conn.send('HTTP/1.1 200 OK\nContent-Type: text/html\n\n'.encode())
      conn.sendall(html)

Based on your request int he comments I have taken it a bit further. Not much though. Just enough to give you an idea what it takes to service different pages.

import socket

def parseRequest(request):
  output = {}
  r = request.decode("utf-8").split("\r\n")
  parts = r[0].split(' ')
  output["method"] = parts[0]
  output["path"] = parts[1]
  output["protocol"] = parts[2]
  output["headers"] = { (kv.split(':')[0]): kv.split(':')[1].strip() for kv in r[1:] if (len(kv.split(':')) > 1) }
  return output

s = socket.socket()
s.bind(('0.0.0.0', 8080))
s.listen(1)

while True:    
  conn, addr = s.accept()
  with conn:
    print('Connected by', addr)
    req = conn.recv(1024)
    r = parseRequest(req)
    path = r["path"][1:]
    if (path == ""): 
      path = "index"
    with open(f'{path}.html', 'rb') as file:
      html = file.read()
      conn.send('HTTP/1.1 200 OK\nContent-Type: text/html\n\n'.encode())
      conn.sendall(html)

You can check the updated repl.

The Fool
  • 16,715
  • 5
  • 52
  • 86
  • Hey man,thanks for the solution.It worked exactly how I intended it to work.the html file (403.html in my case) opened successfully on the port.But, I have linked another html page to (403.html), as a href link ,and it is also in same directory.But the linked page is not opening when clicking on the link – Trapmap Mar 25 '21 at 04:29
  • @Trapmap that becomes fairly complex quickly. Maybe you want to use a framework instead. [Flask](https://flask.palletsprojects.com/en/1.1.x/) is nice and simple. If my answer was helpful to you please consider accepting it and maybe leaving a vote. – The Fool Mar 25 '21 at 06:56
  • In general, you want to read the request and look at the path and open the file that has the same name as the path. I think you will be able to do that if you want. For learning ok but for production I would definitely use a framework. – The Fool Mar 25 '21 at 07:08
  • @Trapmap, I have updated my answer to show a little of how you could to it. – The Fool Mar 25 '21 at 07:46