Skip to content
/ pyts Public

pyts; Python Transfer System. Client-server protocol written in Python

Notifications You must be signed in to change notification settings

glaukiol1/pyts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pyts | Python Transfer System

Data transfering system written in Python. HTTP-like, with headers and body for request/response. It uses sockets for communicating between server and client. Easy programming for endpoints; you can set route handlers with a function, which will give you access to two parameters, request and response. You can use the response arguemnt to set headers and data, and you could call response.SendResponse() to send it back to the client.

pyts | Client

A pyts client is the client in the server-client relationship. It sends requests, that will be responded to by the server. To send a request, you would first need to make the actual request class, and call the request.SendRequest.

Sending a Request

To build the Request, the syntax is as follows; req = Request(HOST,PORT,ENDPOINT)

  • HOST = IP address of the server; a local server would be 127.0.0.1
  • PORT = Port of the pyts service
  • ENDPOINT = the endpoint you are requesting; something like / or /endpoint.

Now that you have built the base request class, and have it in a variable, recomended namely req, it is time to set headers and the body.

An example of setting a header and the body would be;

req.setHeader("TEST-HEADER", True)
req.setBody({"test": True})

Now that our request is all set up, we can now send it. Call the request.SendRequest function. Note that this function returns the response back, so you would use something like;

response = req.SendRequest()

Thats it! Now you have access to the Response class. You have access to all the headers and data sent back. To get the status code, you can use;

response.StatusCode

pyts | Server

The server recives requests on a certain port, and passes the request class and a newly created response class, with all the default values. To send the response back, you could use the response.SendResponse to send the response back.

Server instance

To create a server, you do the following;

app = Server(HOST, PORT)
  • HOST = the host that you are going to run on, most likely 127.0.0.1
  • PORT = the port where the pyts app will listen on.

Route handlers

A route handler is a function that handles a request to a certain endpoint. To define a request handler, you make a function with two paramaters; request and response.

def routeHandlerFunction(request:Request, resp:Response)

In this function, you would do all actions and return a response. Use the functions of the Response class to set the status, headers, and body.

After you have set the fields that you wish to fill out, to send the response, you use response.SendResponse.

A very basic example would be;

def routeHandlerFunction(request:Request, resp:Response):
    resp.setStatus(200, "OK")
    resp.setBody("<1>Index page</1>")
    resp.SendResponse()

Linking the route handler with the server instance

Now, you need to actually link this function to a certain route; which you can achive via;

app.routeHandler(ENDPOINT, routeHandlerFunction)
  • ENDPOINT = the endpoint that this handler will handle,examples; /, /endpoint
  • routeHandlerFunction = the function you created that will handle the request.

Starting the server

To start the server in the host and port you specified when creating the server instance, you can run;

app.startServer()

It takes no paramaters, and will output in the terminal that looks something like this;

INFO:: Server started; HOST:PORT

Of course, HOST and PORT will be subsituted for whatever values you chose in initializing the Server class.

About

pyts; Python Transfer System. Client-server protocol written in Python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published