您好,登錄后才能下訂單哦!
#!/usr/bin/env python
# -- encoding: utf-8 --
'''
@Author : {liush}
@License : (C) Copyright 2018-2037, {liush}
@Contact : {lumia98@vip.qq.com}
@Software: PyCharm
@File : Servers.py
@Time : 2018/9/2 11:28
@Desc : socket服務(wù)端
'''
import socket
ip = '127.0.0.1'
port = 8000
server_socket = socket.socket()
server_socket.bind((ip, port))
server_socket.listen()
while 1:
client_socket, client_addr = server_socket.accept()
print("接收到客戶端{(lán)}的請(qǐng)求,端口{}".format(client_addr[0], client_addr[1]))
data = client_socket.recv(1024)
if data:
print("----->客服端發(fā)來(lái)的數(shù)據(jù){}".format(data.decode('utf-8')))
file = open('data.txt',mode='a+', encoding='utf-8')
file.write(data.decode('utf-8'))
file.close()
else:
break
print("發(fā)送完成")
server_socket.close()
客戶端
import socket
ip = '127.0.0.1'
port = 8000
client_socket = socket.socket() #創(chuàng)建socket對(duì)象
client_socket.connect((ip,port)) #創(chuàng)建連接
print('正在連接{}服務(wù)器,連接端口{}'.format(ip,port))
data = input(">>>>>>>>>>")
client_socket.send(data.encode())
print("連接完成")
client_socket.close()
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。