D
D
Drovosek012019-03-17 21:03:23
linux
Drovosek01, 2019-03-17 21:03:23

On Linux in python, do relative paths work "natively"?

I watch the guide: https://youtu.be/4haMUvUxUJI?t=2055 and there at 34:15 the path to the html file is passed to the open() method. The path is relative and without using os.path Is
this a feature of python on Linux or is the PATH variable set up so that it looks for files in the folder where the included html files are located?
And yet, without the "encoding='utf-8'" argument in the open() method, the html file does not open normally, but everything is fine on the video, is this also a feature of Linux?
Thank you very much for the lesson!
Tell me, please, 2 things.
PS
Still for some reason the file with styles is not loaded. I looked in Chrome DevTools, where the GET request to the css file goes directly to localhost:5000/style.css and then the server crashes with the KeyError: '/style.css' error. I had to write styles directly in the html file. Any ideas how to fix this?

import socket
from views import *


URLS = {
    '/': index,
    '/blog': blog
}

def generate_content(method, url):
    # if method == 404:
    #     return '<h1>404</h1><p>Not found</p>'
    # if method == 405:
    #     return '<h1>405</h1><p>Method not allowed</p>'
    # return '<h1>{}</h1>'.format(URLS[url])
    return URLS[url]()

def generate_headers(method, url):
    if method != 'GET':
        return ('HTTP/1.1 405 Method not allowed\n\n', 405)

    if url not in URLS:
        return('HTTP/1.1 404 Not found\n\n', 404)

    return ('HTTP/1.1 200 OK\n\n', 200)

def parse_request(request):
    parsed = request.split(' ')
    method = parsed[0]
    url = parsed[1]
    parsed_lines = request.split('\r\n\r\n')
    print('-----PARSED_LINE-----', parsed_lines)
    return (method, url)

def generate_response(request):
    method, url = parse_request(request)
    headers, code = generate_headers(method, url)
    body = generate_content(method, url)
    return (headers + body).encode()

def run():
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_socket.bind(('localhost', 5000))
    server_socket.listen(1)

    while True:
        client_socket, addr = server_socket.accept()
        request = client_socket.recv(1024)
        print(request.decode('utf-8'))
        print()
        print(addr)

        response = generate_response(request.decode('utf-8'))
        # client_socket.sendall('hello world'.encode())
        client_socket.sendall(response)
        client_socket.close()

if __name__ == '__main__':
    run()

views.py
import os


dir = os.path.dirname(__file__)

def index():
    with open(os.path.join(dir,'templates/index.html'), encoding='utf-8') as template:
        return template.read()

def blog():
    with open(r'templates/blog.html', encoding='utf-8') as template:
        return template.read()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-03-17
@sergey-gornostaev

I didn't watch the video but

  1. Good Python programs should be cross-plot, so you should avoid delimited paths and use os.path.join()other portable path manipulation tools.
  2. If you are using Python2 then use Python3. And if you are using Python3, then always specify the encoding when opening the file, and always use UTF-8 for encoding files.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question