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

网站制作需求文档无线网站应建设在什么地方

网站制作需求文档,无线网站应建设在什么地方,佛山网站建设推广服务,扁平化设计网站欣赏本节内容 Socket介绍Socket参数介绍基本Socket实例Socket实现多连接处理通过Socket实现简单SSH通过Socket实现文件传送作业#xff1a;开发一个支持多用户在线的FTP程序1. Socket介绍 概念 A network socket is an endpoint of a connection across a computer network. Today…  本节内容 Socket介绍Socket参数介绍基本Socket实例Socket实现多连接处理通过Socket实现简单SSH通过Socket实现文件传送作业开发一个支持多用户在线的FTP程序    1. Socket介绍 概念   A network socket is an endpoint of a connection across a computer network. Today, most communication between computers is based on the Internet Protocol; therefore most network sockets are Internet sockets. More precisely, a socket is a handle (abstract reference) that a local program can pass to the networking application programming interface (API) to use the connection, for example send this data on this socket. For example, to send Hello, world! via TCP to port 80 of the host with address 1.2.3.4, one might get a socket, connect it to the remote host, send the string, then close the socket. 实现一个socket至少要分以下几步(伪代码) 1 2 3 4 Socket socket  getSocket(type  TCP)  #设定好协议类型 connect(socket, address  1.2.3.4, port  80) #连接远程机器 send(socket, Hello, world!) #发送消息 close(socket) #关闭连接 A socket API is an application programming interface (API), usually provided by the operating system, that allows application programs to control and use network sockets. Internet socket APIs are usually based on the Berkeley sockets standard. In the Berkeley sockets standard, sockets are a form of file descriptor (a file handle), due to the Unix philosophy that everything is a file, and the analogies between sockets and files: you can read, write, open, and close both.    A socket address is the combination of an IP address and a port number, much like one end of a telephone connection is the combination of a phone number and a particular extension. Sockets need not have an address (for example for only sending data), but if a program binds a socket to an address, the socket can be used to receive data sent to that address. Based on this address, internet sockets deliver incoming data packets to the appropriate application process or thread. Socket Families(地址簇) socket.AF_UNIX unix本机进程间通信  socket.AF_INET IPV4  socket.AF_INET6  IPV6 These constants represent the address (and protocol) families, used for the first argument to socket(). If the AF_UNIX constant is not defined then this protocol is unsupported. More constants may be available depending on the system. Socket Types socket.SOCK_STREAM  #for tcp socket.SOCK_DGRAM   #for udp  socket.SOCK_RAW     #原始套接字普通的套接字无法处理ICMP、IGMP等网络报文而SOCK_RAW可以其次SOCK_RAW也可以处理特殊的IPv4报文此外利用原始套接字可以通过IP_HDRINCL套接字选项由用户构造IP头。 socket.SOCK_RDM  #是一种可靠的UDP形式即保证交付数据报但不保证顺序。SOCK_RAM用来提供对原始协议的低级访问在需要执行某些特殊操作时使用如发送ICMP报文。SOCK_RAM通常仅限于高级用户或管理员运行的程序使用。 socket.SOCK_SEQPACKET #废弃了 These constants represent the socket types, used for the second argument to socket(). More constants may be available depending on the system. (Only SOCK_STREAM and SOCK_DGRAM appear to be generally useful.)   2. Socket 参数介绍 socket.socket(familyAF_INET, typeSOCK_STREAM, proto0, filenoNone)  必会 Create a new socket using the given address family, socket type and protocol number. The address family should be AF_INET (the default), AF_INET6, AF_UNIX, AF_CAN or AF_RDS. The socket type should beSOCK_STREAM (the default), SOCK_DGRAM, SOCK_RAW or perhaps one of the other SOCK_ constants. The protocol number is usually zero and may be omitted or in the case where the address family is AF_CAN the protocol should be one of CAN_RAW or CAN_BCM. If fileno is specified, the other arguments are ignored, causing the socket with the specified file descriptor to return. Unlike socket.fromfd(), fileno will return the same socket and not a duplicate. This may help close a detached socket using socket.close(). socket.socketpair([family[, type[, proto]]]) Build a pair of connected socket objects using the given address family, socket type, and protocol number. Address family, socket type, and protocol number are as for the socket() function above. The default family is AF_UNIX if defined on the platform; otherwise, the default is AF_INET. socket.create_connection(address[, timeout[, source_address]]) Connect to a TCP service listening on the Internet address (a 2-tuple (host, port)), and return the socket object. This is a higher-level function than socket.connect(): if host is a non-numeric hostname, it will try to resolve it for both AF_INET and AF_INET6, and then try to connect to all possible addresses in turn until a connection succeeds. This makes it easy to write clients that are compatible to both IPv4 and IPv6. Passing the optional timeout parameter will set the timeout on the socket instance before attempting to connect. If no timeout is supplied, the global default timeout setting returned by getdefaulttimeout() is used. If supplied, source_address must be a 2-tuple (host, port) for the socket to bind to as its source address before connecting. If host or port are ‘’ or 0 respectively the OS default behavior will be used. socket.getaddrinfo(host, port, family0, type0, proto0, flags0) #获取要连接的对端主机地址 必会 sk.bind(address) 必会   s.bind(address) 将套接字绑定到地址。address地址的格式取决于地址族。在AF_INET下以元组host,port的形式表示地址。 sk.listen(backlog) 必会   开始监听传入连接。backlog指定在拒绝连接之前可以挂起的最大连接数量。       backlog等于5表示内核已经接到了连接请求但服务器还没有调用accept进行处理的连接个数最大为5      这个值不能无限大因为要在内核中维护连接队列 sk.setblocking(bool) 必会   是否阻塞默认True如果设置False那么accept和recv时一旦无数据则报错。 sk.accept() 必会   接受连接并返回conn,address,其中conn是新的套接字对象可以用来接收和发送数据。address是连接客户端的地址。   接收TCP 客户的连接阻塞式等待连接的到来 sk.connect(address) 必会   连接到address处的套接字。一般address的格式为元组hostname,port,如果连接出错返回socket.error错误。 sk.connect_ex(address)   同上只不过会有返回值连接成功时返回 0 连接失败时候返回编码例如10061 sk.close() 必会   关闭套接字 sk.recv(bufsize[,flag]) 必会   接受套接字的数据。数据以字符串形式返回bufsize指定最多可以接收的数量。flag提供有关消息的其他信息通常可以忽略。 sk.recvfrom(bufsize[.flag])   与recv()类似但返回值是data,address。其中data是包含接收数据的字符串address是发送数据的套接字地址。 sk.send(string[,flag]) 必会   将string中的数据发送到连接的套接字。返回值是要发送的字节数量该数量可能小于string的字节大小。即可能未将指定内容全部发送。 sk.sendall(string[,flag]) 必会   将string中的数据发送到连接的套接字但在返回之前会尝试发送所有数据。成功返回None失败则抛出异常。       内部通过递归调用send将所有内容发送出去。 sk.sendto(string[,flag],address)   将数据发送到套接字address是形式为ipaddrport的元组指定远程地址。返回值是发送的字节数。该函数主要用于UDP协议。 sk.settimeout(timeout) 必会   设置套接字操作的超时期timeout是一个浮点数单位是秒。值为None表示没有超时期。一般超时期应该在刚创建套接字时设置因为它们可能用于连接的操作如 client 连接最多等待5s sk.getpeername()  必会   返回连接套接字的远程地址。返回值通常是元组ipaddr,port。 sk.getsockname()    返回套接字自己的地址。通常是一个元组(ipaddr,port) sk.fileno()    套接字的文件描述符 socket.sendfile(file, offset0, countNone)      发送文件 但目前多数情况下并无什么卵用。   SocketServer The socketserver module simplifies the task of writing network servers. There are four basic concrete server classes: class socketserver.TCPServer(server_address, RequestHandlerClass, bind_and_activateTrue) This uses the Internet TCP protocol, which provides for continuous streams of data between the client and server. If bind_and_activate is true, the constructor automatically attempts to invoke server_bind() andserver_activate(). The other parameters are passed to the BaseServer base class. class socketserver.UDPServer(server_address, RequestHandlerClass, bind_and_activateTrue) This uses datagrams, which are discrete packets of information that may arrive out of order or be lost while in transit. The parameters are the same as for TCPServer. class socketserver.UnixStreamServer(server_address, RequestHandlerClass, bind_and_activateTrue)class socketserver.UnixDatagramServer(server_address, RequestHandlerClass,bind_and_activateTrue) These more infrequently used classes are similar to the TCP and UDP classes, but use Unix domain sockets; they’re not available on non-Unix platforms. The parameters are the same as for TCPServer. These four classes process requests synchronously; each request must be completed before the next request can be started. This isn’t suitable if each request takes a long time to complete, because it requires a lot of computation, or because it returns a lot of data which the client is slow to process. The solution is to create a separate process or thread to handle each request; the ForkingMixIn and ThreadingMixIn mix-in classes can be used to support asynchronous behaviour. There are five classes in an inheritance diagram, four of which represent synchronous servers of four types: ------------ | BaseServer | ------------| v ----------- ------------------ | TCPServer |-------| UnixStreamServer | ----------- ------------------ | v ----------- -------------------- | UDPServer |-------| UnixDatagramServer | ----------- -------------------- Note that UnixDatagramServer derives from UDPServer, not from UnixStreamServer — the only difference between an IP and a Unix stream server is the address family, which is simply repeated in both Unix server classes. class socketserver.ForkingMixInclass socketserver.ThreadingMixIn Forking and threading versions of each type of server can be created using these mix-in classes. For instance, ThreadingUDPServer is created as follows: class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass The mix-in class comes first, since it overrides a method defined in UDPServer. Setting the various attributes also changes the behavior of the underlying server mechanism. class socketserver.ForkingTCPServerclass socketserver.ForkingUDPServerclass socketserver.ThreadingTCPServerclass socketserver.ThreadingUDPServer These classes are pre-defined using the mix-in classes.       Request Handler Objects class socketserver.BaseRequestHandler This is the superclass of all request handler objects. It defines the interface, given below. A concrete request handler subclass must define a new handle() method, and can override any of the other methods. A new instance of the subclass is created for each request. setup() Called before the handle() method to perform any initialization actions required. The default implementation does nothing. handle() This function must do all the work required to service a request. The default implementation does nothing. Several instance attributes are available to it; the request is available as self.request; the client address as self.client_address; and the server instance as self.server, in case it needs access to per-server information. The type of self.request is different for datagram or stream services. For stream services,self.request is a socket object; for datagram services, self.request is a pair of string and socket. finish() Called after the handle() method to perform any clean-up actions required. The default implementation does nothing. If setup() raises an exception, this function will not be called.       socketserver.TCPServer Example server side 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import socketserver class MyTCPHandler(socketserver.BaseRequestHandler):          The request handler class for our server.     It is instantiated once per connection to the server, and must     override the handle() method to implement communication to the     client.          def handle(self):         # self.request is the TCP socket connected to the client         self.data  self.request.recv(1024).strip()         print({} wrote:.format(self.client_address[0]))         print(self.data)         # just send back the same data, but upper-cased         self.request.sendall(self.data.upper()) if __name__  __main__:     HOST, PORT  localhost, 9999     # Create the server, binding to localhost on port 9999     server  socketserver.TCPServer((HOST, PORT), MyTCPHandler)     # Activate the server; this will keep running until you     # interrupt the program with Ctrl-C     server.serve_forever() client side 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import socket import sys HOST, PORT  localhost, 9999 data   .join(sys.argv[1:]) # Create a socket (SOCK_STREAM means a TCP socket) sock  socket.socket(socket.AF_INET, socket.SOCK_STREAM) try:     # Connect to server and send data     sock.connect((HOST, PORT))     sock.sendall(bytes(data  \n, utf-8))     # Receive data from the server and shut down     received  str(sock.recv(1024), utf-8) finally:     sock.close() print(Sent:     {}.format(data)) print(Received: {}.format(received)) 上面这个例子你会发现依然不能实现多并发哈哈在server端做一下更改就可以了 把 1 server  socketserver.TCPServer((HOST, PORT), MyTCPHandler) 改成   1 server  socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler)   转载于:https://www.cnblogs.com/yangliheng/p/6106701.html
http://www.zqtcl.cn/news/87306/

相关文章:

  • 网站建设类营销型网站模板免费下载
  • 网站开发规范织梦网站安装教程
  • 网站制作呼和浩特买卖交易网
  • 昆明网站制作方案定制网站服务器建设软件
  • 培训机构网站阿里云1m 宽带做网站服务器
  • 农业交易平台网站建设国外的包装设计网站
  • 上海装修公司做网站网站被备案能建设
  • 题库网站怎样做wp网站怎么用插件做html网页
  • 做内容网站关键词查询工具哪个好
  • 电脑做ppt一般下载哪个网站好wordpress木马乐主题
  • 网站备案要多少钱外贸平台网站的营销方式
  • 厦门行业网站建设做外贸要自己建网站吗
  • 银行门户网站建设方案向百度提交网站
  • 石家庄网站建设需要多少钱新开传奇网站新开网
  • 深圳做网站(信科网络)电子商务网站的建设方法
  • 建筑公司网站源码下载做好网站建设
  • 免费做网站怎么做网站链接公司内网怎么搭建
  • 找建筑网站团队展示网站
  • 软件开发和网站开发有何不同合肥做公司网站一般多少钱
  • 个人网站开发协议官方网站下载cad
  • 域名被墙检测网站阜新本地网站建设平台
  • 网站优化排名资源网站推广的方法有
  • 网站开发集成软件网站怎么换模板
  • 做盗文网站网站建设需求怎么提
  • 网站建设及规划方案网站开发 职位晋升路线
  • 无锡网站建设品牌大全无极电影网叛逆者
  • 免费商业网站模板嵌入式培训学校
  • 个人注册公司网站空间商城网站免费建设
  • 泰安网站制作哪家好青岛房产网查询
  • 网站模板一样侵权吗帝国做网站怎么加视频