Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to send http request and receive http response by python through get and post

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how python sends http requests and receives http responses through get and post. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

1.GET method

The get method is to put the requested data directly into the url, and then use httplib and urllib2 to simulate the user login.

1)

# URL address

Url_Addr = "https://apac-axlprod01-api.com:8081/userLogin?"

# parameters to be passed when the user logs in

Params = urllib.urlencode ({'name': user_name,' password': user_pwd, 'appId': app_Id})

# there seems to be something wrong with the python certificate I installed, failure check

Ssl._create_default_https_context = ssl._create_unverified_context

# combine parameters and URL into a URL

Req = urllib2.Request (url_Addr+params)

Res = urllib2.urlopen (req)

Data = res.read ()

Res.close ()

2)

# URL address

Url_Addr = "apac-axlprod01-api.com:8081"

# parameters to be passed when the user logs in

Params = urllib.urlencode ({'name': user_name,' password': user_pwd, 'appId': app_Id})

Ssl._create_default_https_context = ssl._create_unverified_context

Conn = httplib.HTTPSConnection (url_Addr)

# combine parameters and URL into a URL

Conn.request ("GET", "/ userLogin?" + params)

Response = conn.getresponse ()

Data = response.read ()

Response.close ()

2.POST method

The POST method is to put the requested data directly in data or body, but not in url. Here, use httplib and urllib2 to simulate user login.

1)

# URL address

Url_Addr = "https://apac-axlprod01-api.com:8081/userLogin"

# parameters to be passed when the user logs in

Params = urllib.urlencode ({'name': user_name,' password': user_pwd, 'appId': app_Id})

Ssl._create_default_https_context = ssl._create_unverified_context

# input URL and Data

Req = urllib2.Request (url = url_Addr,data = params)

Res = urllib2.urlopen (req)

Data = res.read ()

Res.close ()

2)

# URL address

Url_Addr = "apac-axlprod01-api.com"

# headers

Headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}

# parameters to be passed when the user logs in

Params = urllib.urlencode ({'name': user_name,' password': user_pwd, 'appId': app_Id})

Ssl._create_default_https_context = ssl._create_unverified_context

Conn = httplib.HTTPSConnection (url_Addr,8081)

# input URL, body and headers

Conn.request ("POST", "/ userLogin", params,headers)

Response = conn.getresponse ()

Data = response.read ()

Response.close ()

Httplib implements the client protocols of http and https, but in python, the modules urllib and urllib2 encapsulate httplib at a higher level

The common types and methods provided by httplib are described in detail below.

Httplib.HTTPConnection (host [, port [, strict [, timeout]])

The constructor of the HTTPConnection class that represents an interaction with the server, that is, the request / response. The parameter host indicates the server host, for example: www.csdn.net;port is the port number, and the default value is 80; the default value of the parameter strict is false, indicating whether to throw a BadStatusLine exception when the status line returned by the server cannot be parsed (status line) (typical status lines such as HTTP/1.0 200 OK); optional parameter timeout indicates the timeout.

The methods provided by HTTPConnection are:

HTTPConnection.request (method, url [, body [, headers]])

Calling the request method sends a request to the server, and method represents the requested method. Commonly used methods are get and post and head; url represents the url of the requested resource; body represents the data submitted to the server and must be a string (if method is "post", body can be understood as the data in the html form); headers represents the http header of the request.

HTTPConnection.getresponse ()

Get the Http response. The returned object is an instance of HTTPResponse, which is explained below about HTTPResponse.

HTTPConnection.connect ()

Connect to the Http server.

HTTPConnection.close ()

Close the connection to the server.

HTTPConnection.set_debuglevel (level)

Sets the level of the height. The default value of the parameter level is 0, which means that no debugging information is output.

Httplib.HTTPResponse

HTTPResponse represents the server's response to client requests. It is often created by calling HTTPConnection.getresponse (), which has the following methods and properties:

HTTPResponse.read ([amt])

Gets the message body of the response. If you are requesting a normal web page, the method returns the html of the page. The optional parameter amt indicates that the specified byte of data is read from the response stream.

HTTPResponse.getheader (name [, default])

Get the response header. Name represents the name of the header domain (header field). The optional parameter default is returned as the default if the header domain does not exist.

HTTPResponse.getheaders ()

Returns all header information as a list.

HTTPResponse.msg

Get all the response header information.

HTTPResponse.version

Gets the http protocol version used by the server. 11 means http/1.1;10 means http/1.0.

HTTPResponse.status

Gets the status code of the response. For example, 200 indicates that the request is successful.

HTTPResponse.reason

Returns a description of the result that the server processed the request. It's usually "OK".

This is how python sends http requests and receives http responses through get and post. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report