脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python程序调用远程服务的步骤详解

python程序调用远程服务的步骤详解

2021-09-17 00:02nutcore Python

这篇文章主要介绍了python程序调用远程服务的步骤详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

前言

项目是基于python3的PC桌面项目。因为需要对外发布web服务进行数据交换所以需要支持web服务。项目主要使用了GET,POST服务请求。

一、python3中怎样进行发送web请求?

python3使用urllib模块实现web请求,可以支持Get和Post请求。

二、使用步骤

 1.引入python库

?
1
2
3
4
import http.client
import urllib,parser
 
urlPre = '127.0.0.1'

2.GET服务

?
1
2
3
4
5
6
def getToRemote(url):
 conn = http.client.HTTPConnection(urlPre,8082, timeout=10)
 conn.request( "GET" , url)
 response = conn.getresponse()
 conn.close
 return response

3.POST服务

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def postToRemote(url,data):
 params = urllib.parse.urlencode(data)
 # post 请求数据,要带上 Content-type 字段,以告知消息主体以何种方式编码
 headers = { "Content-type" : "application/json"}
 conn = http.client.HTTPConnection(urlPre,8082, timeout=10)
 conn.request( "POST" , url ,params,headers)
 response = conn.getresponse()
 conn.close
 return response
 
    #调用post请求
 cpParams = []
 for i in range(0,len(data),1):
  cpParams.append(data[i]['path'])
  cpParams.append(data[i]['id'])
  cpParams.append(data[i]['name'])
 postToRemote('/copy',{'params':cpParams})

总结

到此这篇关于python程序调用远程服务的步骤详解的文章就介绍到这了,更多相关python程序调用远程服务内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/fitAllEnv/article/details/114461115

延伸 · 阅读

精彩推荐