Use python3 to write a script to send a get or post request, but every time you write a script, you have to search for it, and then copy it, and then test it. Here is a note to use the demo, convenient for future use.

  • A get request
Git repository information
def get_git_info():
params = parse.urlencode({"repo":repo,"git_url":git_url})
response = request.urlopen(conf.git_info_url + '? %s' % params)
result = json.loads(response.read().decode('utf-8'))
global git_info
if result['status'] :print('Warehouse information obtained successfully')
    git_info = result['data']
    global code_run_path
    if code_run_path == ' ':
        code_run_path = git_info['code_run_path'].rstrip('/')
else:
    msg = result['msg']
    print(msg)
    raise Exception('Failed to get git repository information')
return git_infoCopy the code
  • From urllib import parse,request, parse.urlencode({“repo”:repo,” git_URL “:git_url}) request.urlopen(conf.git_info_url + ‘? %s’ % params) sends a concatenation request, response.read().decode(‘ utF-8 ‘) reads the returned content and parses it in UTF-8 encoding
  • A post request
def log_release(success_servers,fail_servers,error_msg,status):
deploy_file = source_dir + '/post-deploy-script.sh'
deploy_script=' '
if os.path.isfile(deploy_file):
    with open(deploy_file,'r') as f:
        deploy_script = f.read()
exclude_file = source_dir + '/.deploy-exclude.list'
exclude_content = ' '
if os.path.isfile(exclude_file):
    with open(exclude_file, 'r') as f:
        exclude_content = f.read()

deploy_version_log = get_version()
data = {"env":env,"mode":mode,"git_url":git_url,"source_dir":source_dir,"is_sync_delete":str(is_sync_delete),"repo_dir":repo,
        "code_run_path":code_run_path,"success_servers":success_servers,"fail_servers":fail_servers,"error_msg":error_msg,
        "status":str(status),"deploy_script":deploy_script,"deploy_version_log":deploy_version_log,"exclude_content":exclude_content}

text_data = json.dumps(data)
text_data = bytes(text_data,'utf-8')
response = request.Request(conf.log_release_url, text_data)
res = request.urlopen(response)Copy the code
Defining JSON parameters

data =
{"env":env,"mode":mode,"git_url":git_url,"source_dir":source_dir,"is_sync_delete":str(is_sync_del
ete),"repo_dir":repo,

"code_run_path":code_run_path,"success_servers":success_servers,"fail_servers":fail_servers,"erro
r_msg":error_msg,

"status":str(status),"deploy_script":deploy_script,"deploy_version_log":deploy_version_log,"exclu
de_content":exclude_content}Copy the code
Convert JSON to a string text_data = json.dumps(data)

Text_data = bytes(text_data,’ utF-8 ‘)

Sending a request Response = request.request (conf.log_release_URL, text_data) res = request.urlopen(response)

It is important to note that past data posted to PHP should be received and processed in the following manner

$data = file_get_contents('php://input');
$data = json_decode($data.true);Copy the code
  • I’ve stumbled on this a few times, using the frame post to retrieve the entire string, and using JSON to parse the parameter array
  • Words or not to say, if there are still problems not solved, can add technology group 887934385 group to get relevant information

Or scan the following QR code to add the assistant customer service sister wechat, reply immediately after passing, provide relevant Python materials

Follow the wechat public account “Python Community Camp” to learn technical know-how in the first time