日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python bind sock_python SOCKET编程详细介绍

發布時間:2023/12/4 python 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python bind sock_python SOCKET编程详细介绍 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本章內容

1、socket

2、IO多路復用

3、socketserver

Socket

socket起源于Unix,而Unix/Linux基本哲學之一就是“一切皆文件”,對于文件用【打開】【讀寫】【關閉】模式來操作。socket就是該模式的一個實現,socket即是一種特殊的文件,一些socket函數就是對其進行的操作(讀/寫IO、打開、關閉)

基本上,Socket 是任何一種計算機網絡通訊中最基礎的內容。例如當你在瀏覽器地址欄中輸入 http://www.cnblogs.com/ 時,你會打開一個套接字,然后連接到 http://www.cnblogs.com/ 并讀取響應的頁面然后然后顯示出來。而其他一些聊天客戶端如 gtalk 和 skype 也是類似。任何網絡通訊都是通過 Socket 來完成的。

Python 官方關于 Socket 的函數請看 http://docs.python.org/library/socket.html

socket和file的區別:

1、file模塊是針對某個指定文件進行【打開】【讀寫】【關閉】

2、socket模塊是針對 服務器端 和 客戶端Socket 進行【打開】【讀寫】【關閉】

那我們就先來創建一個socket服務端吧

服務端代碼

import socket

sk = socket.socket()

sk.bind(("127.0.0.1",8080))

sk.listen(5)

conn,address = sk.accept()

sk.sendall(bytes("Hello world",encoding="utf-8"))

客戶端代碼

import socket

obj = socket.socket()

obj.connect(("127.0.0.1",8080))

ret = str(obj.recv(1024),encoding="utf-8")

print(ret)

socket更多功能

def bind(self, address): # real signature unknown; restored from __doc__

"""

bind(address)

Bind the socket to a local address.? For IP sockets, the address is a

pair (host, port); the host must refer to the local host. For raw packet

sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])

"""

'''將套接字綁定到本地地址。是一個IP套接字的地址對(主機、端口),主機必須參考本地主機。'''

pass

def close(self): # real signature unknown; restored from __doc__

"""

close()

Close the socket.? It cannot be used after this call.

"""

'''關閉socket'''

pass

def connect(self, address): # real signature unknown; restored from __doc__

"""

connect(address)

Connect the socket to a remote address.? For IP sockets, the address

is a pair (host, port).

"""

'''將套接字連接到遠程地址。IP套接字的地址'''

pass

def connect_ex(self, address): # real signature unknown; restored from __doc__

"""

connect_ex(address) -> errno

This is like connect(address), but returns an error code (the errno value)

instead of raising an exception when an error occurs.

"""

pass

def detach(self): # real signature unknown; restored from __doc__

"""

detach()

Close the socket object without closing the underlying file descriptor.

The object cannot be used after this call, but the file descriptor

can be reused for other purposes.? The file descriptor is returned.

"""

'''關閉套接字對象沒有關閉底層的文件描述符。'''

pass

def fileno(self): # real signature unknown; restored from __doc__

"""

fileno() -> integer

Return the integer file descriptor of the socket.

"""

'''返回整數的套接字的文件描述符。'''

return 0

def getpeername(self): # real signature unknown; restored from __doc__

"""

getpeername() -> address info

Return the address of the remote endpoint.? For IP sockets, the address

info is a pair (hostaddr, port).

"""

'''返回遠程端點的地址。IP套接字的地址'''

pass

def getsockname(self): # real signature unknown; restored from __doc__

"""

getsockname() -> address info

Return the address of the local endpoint.? For IP sockets, the address

info is a pair (hostaddr, port).

"""

'''返回遠程端點的地址。IP套接字的地址'''

pass

def getsockopt(self, level, option, buffersize=None): # real signature unknown; restored from __doc__

"""

getsockopt(level, option[, buffersize]) -> value

Get a socket option.? See the Unix manual for level and option.

If a nonzero buffersize argument is given, the return value is a

string of that length; otherwise it is an integer.

"""

'''得到一個套接字選項'''

pass

def gettimeout(self): # real signature unknown; restored from __doc__

"""

gettimeout() -> timeout

Returns the timeout in seconds (float) associated with socket

operations. A timeout of None indicates that timeouts on socket

operations are disabled.

"""

'''返回的超時秒數(浮動)與套接字相關聯'''

return timeout

def ioctl(self, cmd, option): # real signature unknown; restored from __doc__

"""

ioctl(cmd, option) -> long

Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are

SIO_RCVALL:? 'option' must be one of the socket.RCVALL_* constants.

SIO_KEEPALIVE_VALS:? 'option' is a tuple of (onoff, timeout, interval).

"""

return 0

def listen(self, backlog=None): # real signature unknown; restored from __doc__

"""

listen([backlog])

Enable a server to accept connections.? If backlog is specified, it must be

at least 0 (if it is lower, it is set to 0)

注:小編知道大家懶,所以把全部功能的中文標記在每個功能的下面啦。下面擼主列一些經常用到的吧

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是形式為(ipaddr,port)的元組,指定遠程地址。返回值是發送的字節數。該函數主要用于UDP協議。

sk.settimeout(timeout)

設置套接字操作的超時期,timeout是一個浮點數,單位是秒。值為None表示沒有超時期。一般,超時期應該在剛創建套接字時設置,因為它們可能用于連接的操作(如 client 連接最多等待5s )

sk.getpeername()

返回連接套接字的遠程地址。返回值通常是元組(ipaddr,port)。

sk.getsockname()

返回套接字自己的地址。通常是一個元組(ipaddr,port)

sk.fileno()

套接字的文件描述符

WEB服務應用:

#!/usr/bin/env python

#coding:utf-8

import socket

def handle_request(client):

buf = client.recv(1024)

client.send("HTTP/1.1 200 OK\r\n\r\n")

client.send("Hello, World")

def main():

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

sock.bind(('localhost',8080))

sock.listen(5)

while True:

connection, address = sock.accept()

handle_request(connection)

connection.close()

if __name__ == '__main__':

main()

總結

以上是生活随笔為你收集整理的python bind sock_python SOCKET编程详细介绍的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。