一、socket编程
1、tcp通信
# server
import socket
ip = '127.0.0.1'
port = 8888
server_address = (ip, port)
server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_sock.bind(server_address)
# 最多可建立5个连接
server_sock.listen(5)
print('bind success')
conn_sock, client_address = server_sock.accept()
print('got connected from %s:%s' % client_address)
msg = 'bye' + '\r\n'
conn_sock.send(msg.encode('utf-8'))
ra = conn_sock.recv(512)
print(ra)
conn_sock.close()
server_sock.close()
# client
import socket
ip = '127.0.0.1'
port = 8888
client_address = (ip, port)
client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_sock.connect(client_address)
data = client_sock.recv(512)
print('the data received is ' + data.decode('utf-8'))
msg = 'hi' + '\r\n'
client_sock.send(msg.encode('utf-8'))
client_sock.close()
2、udp通信
#!/usr/bin/python3
# 文件名:server.py
# 导入 socket、sys 模块
import socket
# import sys
# 创建 socket 对象
server_socket = socket.socket(
socket.AF_INET, socket.SOCK_DGRAM)
# 获取本地主机名
host = socket.gethostname()
# print(host)
# host = '127.0.0.1'
port = 9999
# 绑定端口号
server_socket.bind((host, port))
print('bind success')
while True:
# 建立客户端连接
data, client_address = server_socket.recvfrom(1024)
print("连接地址: ", str(client_address))
msg = '欢迎!' + "\r\n"
server_socket.sendto(msg.encode('utf-8'), client_address)
server_socket.close()
#!/usr/bin/python3
# 文件名:client.py
# 导入 socket、sys 模块
import socket
# import sys
# 创建 socket 对象
client_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# 获取本地主机名
host = socket.gethostname()
# print(host)
# host = '127.0.0.1'
# 设置端口号
port = 9999
msg = 'hi' + '\r\n'
client_sock.sendto(msg.encode('utf-8'), (host, port))
data, server_address = client_sock.recvfrom(1024)
client_sock.close()
print(data.decode('utf-8'))
一、http通信
1、requests方法
#!/usr/bin/python3
# get方法
import requests
html = requests.get('http://www.baidu.com')
print(html.content)
#!/usr/bin/python3
# post方法
import requests
# 以下为POST请求
url = 'https://open.lixinger.com/api/a/stock/industry'
data = {
"token": "b943f9aa-33f7-40e2-b919-a9b577b77cc0",
"stockCode": "000028"
}
requests = requests.post(url, data=data)
print(requests.content) # 返回字节形式
#!/usr/bin/python3
# 返回状态
import requests
url = 'http://www.baidu.com'
r = requests.get(url)
if r.status_code == requests.codes.ok:
print('status_code : ', r.status_code) # 响应码
print('headers : ', r.headers) # 响应头
print('Content-Type : ', r.headers.get('Content-Type')) # 获取响应头中的Content-Type字段
else:
r.raise_for_status() # 抛出异常
# put/delete/head/options也差不多
r = requests.put(url, data={'key': 'value'})
r = requests.delete(url)
r = requests.head(url)
r = requests.options(url)
# 其它形式
# 1、如www.baidu.com/get?key=val。
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get("http://www.baidu.com/get", params=payload)
print(r.url)
#2、如定制请求头
headers = {'user-agent': 'my-app/0.0.1'}
r= requests.get("http://www.baidu.com",headers=headers)
#3、如需要带cookies来完成的
r0 = requests.get("http://www.baidu.com/login")
r1 = requests.post("http://www.baidu.com/asset",data={"hostname":"xxx",cookies= r0.cookies}
2、urllib方法
import urllib.request
url = 'http://www.baidu.com'
response = urllib.request.urlopen(url)
xml = response.read().decode('utf-8')
print(xml)
3、http.client方法
import http.client
con = http.client.HTTPConnection('www.baidu.com')
con.request("GET", "/index.html", '', {})
res = con.getresponse()
print(res.status, res.reason, res.info()) # 打印读取到的数据