Python2通过TCP发送xml报文

Python2通过TCP发送xml报文

示例代码

# -*- coding: utf-8 -*-
import socket

# 使用tcp发送请求报文
def tcpsend(ip, port, xmlbw):
    """
    ip: ip地址
    port: 端口号
    xmlbw: xml字符串
    """
    address = (ip, port)
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect(address)
    by = xmlbw.encode('utf8') # 转为字节数组

   # print by
   client.send(by)
   print "tcp发送成功"
   data = client.recv(1024) # 接受响应信息
   print "接受响应成功"
   print data
    
   client.close()
Last modification:April 10th, 2019 at 10:07 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment