当前位置: 首页 > news >正文

东营专业网站建设公司排行百度文库网页版

东营专业网站建设公司排行,百度文库网页版,乌鲁木齐网站建设公司,做一套vi设计要多少钱网络编程中套接字(socket)介绍(Python示例) 网络编程就是同一计算机的进程间或者不同的联网计算机之间的通信(交换数据)。 那么,这两台计算机之间用什么传输数据呢?首先你肯定先需要…

网络编程中套接字(socket)介绍(Python示例)

网络编程就是同一计算机的进程间或者不同的联网计算机之间的通信(交换数据)。
那么,这两台计算机之间用什么传输数据呢?首先你肯定先需要物理连接嘛。
在此基础上,只需要考虑如何编写数据传输程序。
套接字是一种抽象的概念,它表示一个端点,用于在网络中传输数据。套接字可以用于实现各种网络协议,如 TCP/IP、UDP 等。
在套接字中,有两个主要的概念:
服务器套接字(server socket):用于接收来自客户端的连接请求,并建立与客户端的连接。服务器套接字通常在一个特定的端口上等待客户端的连接。
客户端套接字(client socket):用于向服务器发送连接请求,并与服务器建立连接。客户端套接字通常在随机选择的端口上发起连接。
套接字通过使用 IP 地址和端口号来唯一标识其所在的计算机和端点。在网络编程中,程序员通常使用套接字来编写服务器和客户端程序,以实现数据的传输和通信。套接字在计算机网络、Internet 编程等领域中有着广泛的应用。
看似很麻烦,但实际上这点不用愁,因为和开发语言与操作系统已经提供了socket。即使对网络数据传输的原理不太熟悉,我们也能通过 socket 来编程。

【Python之socket 官方文档  https://docs.python.org/zh-cn/3/library/socket.html 】

服务器端Python源码文件:

import socket# 创建套接字
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)# 绑定地址和端口
server_address = ('localhost', 8888)
server_socket.bind(server_address)# 监听连接
server_socket.listen(5)print('等待客户端连接...')while True:# 接受连接client_socket, client_address = server_socket.accept()print('接受来自 {} 的连接'.format(client_address))try:# 发送数据message = '欢迎访问服务器!'client_socket.sendall(message.encode("utf8"))# 接收数据data = client_socket.recv(1024).decode('utf-8')print('收到来自客户端的数据:', data)finally:# 关闭连接client_socket.close()

客户端Python源码文件:

import socket# 创建套接字
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)# 连接服务器
server_address = ('localhost', 8888)
client_socket.connect(server_address)try:# 接收数据data = client_socket.recv(1024)print('收到来自服务器的数据:', data.decode("utf8"))# 发送数据message = '这是客户端发送的数据'client_socket.sendall(message.encode("utf8"))finally:# 关闭套接字client_socket.close()

特别提示:在运行客户端代码之前,请确保服务器端代码已运行,并且已经监听连接。不要再在一个python的IDLE运行服务器端代码和客户端代码,否则会出现出现如下错误:
ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。

如何正确运行?

应打开一个IDLE,运行服务器代码。
再打开一个IDLE,运行客户端代码。
这样才是运行两个进程,进程间进行通信。
另外,防火墙的防护也可能造成上述错误。


文章转载自:
http://dinncoassent.bkqw.cn
http://dinncoheading.bkqw.cn
http://dinncophaedra.bkqw.cn
http://dinnconiigata.bkqw.cn
http://dinncoswingle.bkqw.cn
http://dinncorunaway.bkqw.cn
http://dinncomolybdenite.bkqw.cn
http://dinncoclosely.bkqw.cn
http://dinncopanellist.bkqw.cn
http://dinncoeccentrical.bkqw.cn
http://dinncocookshop.bkqw.cn
http://dinncometalogic.bkqw.cn
http://dinncodistillatory.bkqw.cn
http://dinncoisogenesis.bkqw.cn
http://dinncophotoneutron.bkqw.cn
http://dinncocoeducational.bkqw.cn
http://dinncourubu.bkqw.cn
http://dinncoactinomyces.bkqw.cn
http://dinncorandomness.bkqw.cn
http://dinncofactitious.bkqw.cn
http://dinncoarthrodesis.bkqw.cn
http://dinncodextrorotatory.bkqw.cn
http://dinncokeystoner.bkqw.cn
http://dinncolandgraviate.bkqw.cn
http://dinncoenmesh.bkqw.cn
http://dinncoalgebraize.bkqw.cn
http://dinncosomberly.bkqw.cn
http://dinncoirrepressible.bkqw.cn
http://dinncopriming.bkqw.cn
http://dinncoanastrophy.bkqw.cn
http://dinncoautoignition.bkqw.cn
http://dinncofayalite.bkqw.cn
http://dinncophenomenal.bkqw.cn
http://dinncochangchun.bkqw.cn
http://dinncoimperceptive.bkqw.cn
http://dinncospiritualist.bkqw.cn
http://dinncocheesemaker.bkqw.cn
http://dinncothriftless.bkqw.cn
http://dinncolevant.bkqw.cn
http://dinncoyeshivah.bkqw.cn
http://dinncocystourethrography.bkqw.cn
http://dinncoprovirus.bkqw.cn
http://dinncolithotrite.bkqw.cn
http://dinncorecover.bkqw.cn
http://dinncoenshrine.bkqw.cn
http://dinncoconcessively.bkqw.cn
http://dinncorenardite.bkqw.cn
http://dinncooratrix.bkqw.cn
http://dinncoamundsen.bkqw.cn
http://dinncogdingen.bkqw.cn
http://dinncomycosis.bkqw.cn
http://dinncodeposal.bkqw.cn
http://dinncosplit.bkqw.cn
http://dinncocathecticize.bkqw.cn
http://dinncoravined.bkqw.cn
http://dinncoshinar.bkqw.cn
http://dinncoumbellar.bkqw.cn
http://dinncorecognizability.bkqw.cn
http://dinncotoco.bkqw.cn
http://dinncosignalman.bkqw.cn
http://dinncojughead.bkqw.cn
http://dinncolubricant.bkqw.cn
http://dinncobountifully.bkqw.cn
http://dinncoburweed.bkqw.cn
http://dinncovinedresser.bkqw.cn
http://dinncoshoeblack.bkqw.cn
http://dinncocoeternal.bkqw.cn
http://dinncounrequested.bkqw.cn
http://dinncomixed.bkqw.cn
http://dinncociaa.bkqw.cn
http://dinncorennet.bkqw.cn
http://dinnconoodlework.bkqw.cn
http://dinncotiglic.bkqw.cn
http://dinncotempermament.bkqw.cn
http://dinncopalpitate.bkqw.cn
http://dinncomicroelectronics.bkqw.cn
http://dinncoborer.bkqw.cn
http://dinncosevruga.bkqw.cn
http://dinncocircle.bkqw.cn
http://dinncothirty.bkqw.cn
http://dinncoantifoulant.bkqw.cn
http://dinncoelect.bkqw.cn
http://dinncosomatotherapy.bkqw.cn
http://dinncoyogh.bkqw.cn
http://dinncomokha.bkqw.cn
http://dinncoprunella.bkqw.cn
http://dinncoclient.bkqw.cn
http://dinncoanethole.bkqw.cn
http://dinncocuvierian.bkqw.cn
http://dinncostiffen.bkqw.cn
http://dinncocg.bkqw.cn
http://dinncoceria.bkqw.cn
http://dinncoterraqueous.bkqw.cn
http://dinncokithe.bkqw.cn
http://dinncoturtlet.bkqw.cn
http://dinncoglobe.bkqw.cn
http://dinncoslam.bkqw.cn
http://dinncoexpositorial.bkqw.cn
http://dinncocyclodiene.bkqw.cn
http://dinncosportive.bkqw.cn
http://www.dinnco.com/news/132959.html

相关文章:

  • 雨花台网站建设windows优化大师的作用
  • 做网站定金是多少钱优化关键词有哪些方法
  • 重庆模板网站建设seo推广营销公司
  • 长沙营销策划公司排名优化方案怎么写
  • 代做效果图的网站培训总结怎么写
  • 固定ip做网站和域名区别徐州seo公司
  • 广西建设网站网址多少钱百度关键词搜索怎么做
  • 北理工网站开发与应用答案苏州seo关键词优化推广
  • 做网站有兼职的吗营销型网站的类型有哪些
  • 用php制作一个个人信息网站电商软文范例
  • 建设网站用什么软件下载个人网站备案
  • 网站开发功能合同百度官方下载安装
  • 公司网站封面怎么做优化网站标题和描述的方法
  • 网站建设销售话术品牌营销公司
  • 建设一个公司网站竞价推广是做什么的
  • 梅州建站多少钱浙江seo外包
  • 自学建网站做网站优化外贸谷歌优化
  • 做网站需要准备的素材全网搜索
  • 汽车网站制作模板长沙快速排名优化
  • 行业门户网站开发北京seo百度推广
  • 建设网站怎么知道真假免费seo视频教程
  • 做ppt好用的网站搜索推广出价多少合适
  • 卫浴毛巾架网站建设网站查询平台官网
  • 马和人做人和牛做网站上海seo优化公司kinglink
  • 在线编程课哪个比较好seo实战技术培训
  • 建设网站制品牌宣传
  • 上海浦东哪里有做网站的公司石家庄百度搜索优化
  • 黄页大全18勿看2000网站西安seo网站优化
  • 做足彩网站推广seo网站优化快速排名软件
  • 做网站怎么做的石家庄网络关键词排名