Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a server in python

How to run a server in python? I already have tried:

python -m SimpleHTTPServer
python -m HTTPServer

but its says to me:

invalid syntax

Can someone help me? Thanks!

like image 332
CALISVALIS1010 Avatar asked Sep 08 '25 04:09

CALISVALIS1010


2 Answers

You can use this command in cmd or terminal

python -m SimpleHTTPServer <port_number>    # Python 2.x

Python 3.x

python3 -m http.server     # Python 3x

By default, this will run the contents of the directory on a local web server, on port 8000. You can go to this server by going to the URL localhost:8000 in your web browser.

like image 60
Hirusha Fernando Avatar answered Sep 09 '25 19:09

Hirusha Fernando


I have made a remote access program that uses the Socket module. If you want to copy the code, that's fine. EDIT: You will need to run it using a cmd file like this: "python (filename).py." After that, you will need to add the line "pause"

#SERVER
import os
import socket

s = socket.socket()
host = socket.gethostname()
port = 8080
s.bind((host, port))
print("Server started at: ", host)
s.listen(1)
conn,addr = s.accept()
print(addr, "connected")


#CLIENT
import os
import socket

s = socket.socket()
port = 8080
host = "YOUR DESKTOP ID" (Your server should say it. I.E. "Server started at: (Desktop-123456)")