简体   繁体   中英

Handling get requests in python

how can I use the IP address of the client that sends the get request? My goal is to create unique file for each client in which files would be named as the ip address of clients. Sorry I haven't been using Python for a while.

Read the docs. If you plan to use the standard library HTTPServer, you can see that is will send you the client address in your handlers:

BaseHTTPRequestHandler

class BaseHTTPServer.BaseHTTPRequestHandler(request, client_address, server)
This class is used to handle the HTTP requests that arrive at the server. By itself, it cannot respond to any actual HTTP requests; it must be subclassed to handle each request method (eg GET or POST). BaseHTTPRequestHandler provides a number of class and instance variables, and methods for use by subclasses.

The handler will parse the request and the headers, then call a method specific to the request type. The method name is constructed from the request. For example, for the request method SPAM, the do_SPAM() method will be called with no arguments. All of the relevant information is stored in instance variables of the handler. Subclasses should not need to override or extend the init () method.

...
client_address:
Contains a tuple of the form (host, port) referring to the client's address.

SimpleHTTPRequestHandler

class SimpleHTTPServer.SimpleHTTPRequestHandler(request, client_address, server)

You can import requests module as follows.

import requests

r = requests.get('https://api.github.com/user',

auth=('user', 'pass'))

r.status_code

r.encoding

r.json()

Much more information can be found with this documentation: http://docs.python-requests.org/en/master/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM