當前位置:首頁 » 編程語言 » pythonudp

pythonudp

發布時間: 2022-01-23 13:57:05

A. 為什麼使用python發送UDP數據,發送的時候使用的卻是QUIC協議

1. 因為目標埠是 80 埠。
Wireshark 的 QUIC filter 僅僅就是
udp.port == 80 || udp.port == 443
2. 不要相信 Wireshark 的協議分析,因為 dissect 經常會出現偏差。

B. python udp可以遠程執行cmd命令嗎

1 import paramiko,os,sys
2
3 ip = raw_input("input ip address :>>>")
4 password = raw_input("input password:>>>")
5 cmd = raw_input("input your cmd:>>> ")
6
7 print '''
8 ------connecting to %s ,--------
9 '''%ip
10 def ssh_cmd(ip,port,cmd,user,passwd):
11 result = ""
12 try:
13 ssh = paramiko.SSHClient()
14 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
15 ssh.connect(ip,port,user,passwd)
16 stdin, stdout, stderr = ssh.exec_command(cmd)
17 result = stdout.read()
18 print result
19 ssh.close()
20 except:
21 print "ssh_cmd err."
22 return result

C. Python用UDP模擬QQ問題

這個問題很有意思!我也想了很久。如果大家了解UDP和TCP的區別。很多人會告訴你。TCP協議因為是可靠的!它會驗證信息是否發送到目的地。而且TCP連接和斷開時都會經過看似漫長3次握手。而UDP因為是不可靠的 它沒有驗證機制更沒有什麼握手打招呼!我們聊天肯定希望速度越快越好不要有延時,所以有很多人或許會告訴你UDP比TCP快得多!所以QQ用UPD。 這個從理論上講是對的!但大家忽視了一個客觀因素。也就是TCP的驗證和3次握手到底能用多少時間,這里可以告訴你最多不會超過0.5秒也就是500毫秒 這個速度玩游戲的人都知道很慢了。如果說0.5秒你覺得我估計少了那麼1秒怎麼也夠了吧 對於我們人來說1秒不是眨眼的功夫嗎。所以說以上所說不是QQ使用UDP的原因,起碼不是主要原因。(其實QQ也有驗證機制,只不過這個驗證機制是由應用層其他協議完成的)那麼就要更深一層去探討UDP和TCP 在傳輸上區別了。首先我個人認為最主要是UPD包比TCP包要少12個位元組。12個位元組差距雖然很微小 但量變到一定程度那就有質的變化。現在我們2M寬頻理論256KB 每秒,就算200KB吧 等於1600個位元組 大家可以算這個差距。還有一個更深層原因也就是UDP「素質」很差。TCP就很紳士。 為什麼說UDP素質不高 是因為UDP就像馬路那種見縫插幀的司機 誰也不讓 拼了命往前沖 不管你路上有多擠 更不會去想維持一下次序。TCP就不會!一旦TCP覺得路上擁擠就會自覺降低發送速度 他會努力維護次序。這樣一進一退差距就大了。所以我覺得這才是QQ使用UDP的主要原因!也似乎也符合騰訊一貫作風 呵呵 MSN使用TCP 就要慢一些 不信你可以試試

D. python3套接字udp設置接受數據超時

Sometimes,you need to manipulate the default values of certain properties of a socket library, for example, the socket timeout.

設定並獲取默認的套接字超時時間。

1.代碼

1 import socket
2
3
4 def test_socket_timeout():
5 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
6 print("Default socket timeout: %s" % s.gettimeout())
7 # 獲取套接字默認超時時間
8 s.settimeout(100)
9 # 設置超時時間
10 print("Current socket timeout: %s" % s.gettimeout())
11 # 讀取修改後的套接字超時時間
12
13
14 if __name__ == '__main__':
15 test_socket_timeout()
2. AF_INET和SOCK_STREAM解釋

1 # 地址簇
2 # socket.AF_INET IPv4(默認)
3 # socket.AF_INET6 IPv6
4 # socket.AF_UNIX 只能夠用於單一的Unix系統進程間通信
5
6 # socket.SOCK_STREAM(數據流) 提供面向連接的穩定數據傳輸,即TCP/IP協議.多用於資料(如文件)傳送。
3.gettimeout()和settimeout()解釋

1 def gettimeout(self): # real signature unknown; restored from __doc__
2 """
3 gettimeout() -> timeout
4
5 Returns the timeout in seconds (float) associated with socket
6 operations. A timeout of None indicates that timeouts on socket
7 operations are disabled.
8 """
9 return timeout
10
11
12 def settimeout(self, timeout): # real signature unknown; restored from __doc__
13 """
14 settimeout(timeout)
15
16 Set a timeout on socket operations. 'timeout' can be a float,
17 giving in seconds, or None. Setting a timeout of None disables
18 the timeout feature and is equivalent to setblocking(1).
19 Setting a timeout of zero is the same as setblocking(0).
20 """
21 pass
22 # 設置套接字操作的超時期,timeout是一個浮點數,單位是秒。值為None表示沒有超時期。
23 # 一般,超時期應該在剛創建套接字時設置,因為它們可能用於連接的操作(如 client 連接最多等待5s )
4.運行結果

1 Default socket timeout: None
2 Current socket timeout: 100.0

E. 如何用python方法檢測UDP埠

本文實例講述了python檢測遠程udp埠是否打開的方法。分享給大家供大家參考。具體實現方法如下:

復制代碼代碼如下:
import socket
import threading
import time
import struct
import Queue
queue = Queue.Queue()
def udp_sender(ip,port):
try:
ADDR = (ip,port)
sock_udp = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock_udp.sendto("abcd...",ADDR)
sock_udp.close()
except:
pass
def icmp_receiver(ip,port):
icmp = socket.getprotobyname("icmp")
try:
sock_icmp = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
except socket.error, (errno, msg):
if errno == 1:
# Operation not permitted
msg = msg + (
" - Note that ICMP messages can only be sent from processes"
" running as root."
)
raise socket.error(msg)
raise # raise the original error
sock_icmp.settimeout(3)
try:
recPacket,addr = sock_icmp.recvfrom(64)
except:
queue.put(True)
return
icmpHeader = recPacket[20:28]
icmpPort = int(recPacket.encode('hex')[100:104],16)
head_type, code, checksum, packetID, sequence = struct.unpack(
"bbHHh", icmpHeader
)
sock_icmp.close()
if code == 3 and icmpPort == port and addr[0] == ip:
queue.put(False)
return
def checker_udp(ip,port):
thread_udp = threading.Thread(target=udp_sender,args=(ip,port))
thread_icmp = threading.Thread(target=icmp_receiver,args=(ip,port))
thread_udp.daemon= True
thread_icmp.daemon = True
thread_icmp.start()
time.sleep(0.1)
thread_udp.start()

thread_icmp.join()
thread_udp.join()
return queue.get(False)
if __name__ == '__main__':
import sys
print checker_udp(sys.argv[1],int(sys.argv[2]))

希望本文所述對大家的Python程序設計有所幫助。

F. python中使用socket編程,如何能夠通過UDP傳遞一個列表類型的數據

Python中的 list 或者 dict 都可以轉成JSON字元串來發送,接收後再轉回來。


首先

importjson

然後,把 list 或 dict 轉成 JSON

json_string=json.mps(list_or_dict)

如果你用的是Python3,這里的 json_string 會是 str 類型(即Python2的unicode類型),可能需要編碼一下:

if type(json_string) == six.text_type:

json_string = json_string.encode('UTF-8')

用socket發送過去,例如

s.sendto(json_string,address)


對方用socket接收,例如

json_string,addr=s.recvfrom(2048)

把JSON轉成 list 或 dict

list_or_dict=json.loads(json_string)




下面是個完整的例子:


client.py

#!/usr/bin/envpython
#-*-coding:UTF-8-*-

importsocket
importjson
importsix

address=('127.0.0.1',31500)
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
mylist=[1,2,3,4,5,6,7,8,9,10]
json_string=json.mps(mylist)
iftype(json_string)==six.text_type:
json_string=json_string.encode('UTF-8')
s.sendto(json_string,address)
s.close()


server.py

#!/usr/bin/envpython
#-*-coding:UTF-8-*-

importsocket
importjson

address=('127.0.0.1',31500)
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind(address)
json_string,addr=s.recvfrom(2048)
mylist=json.loads(json_string)
print(mylist)
s.close()


請先運行server.py,再運行client.py

G. python socket 發送udp和tcp的區別

Python中的 list 或者 dict 都可以轉成JSON字元串來發送,接收後再轉回來。 首先 import json然後,把 list 或 dict 轉成 JSON json_string = json.mps(list_or_dict)用socket發送過去,例如 s.sendto(json_string, address) 對方用socket接收...

H. python udp recv接收的是位元組流還是字元串

socket一般發送的是字元串,你要想用位元組流的話,轉換一下。
針對你說的情況
發送:
比如可以但是不限於用struct模塊的struct.pack方法,把字元串打包成位元組流再發送,具體用法你可以搜一下。
接收:
首先確定對方發過來的是位元組流,你才能用struct.unpack解包,不然是不行的。
我之前還遇到過將字元串轉ascii碼,再轉16進制碼,然後進行交互的,所以你可以試試。

I. python設計UDP通信時,recvfrom()中的參數是什麼意思

socket.recvfrom(bufsize[, flags])

Receive data from the socket. The return value is a pair (bytes, address) where bytes is a bytes object
representing the data received and address is the address of the socket
sending the data. See the Unix manual page recv(2) for
the meaning of the optional argument flags; it defaults to zero. (The
format of address depends on the address family — see above.)

recvfrom(1)就是從緩沖區讀一個位元組的數據

J. python實現多地址UDP同時,高效接收

每個udp socket只能bind到一個本地埠上。你可以多搞幾個,然後加select就可以了。

熱點內容
關閉androidstudio 發布:2025-02-14 03:53:42 瀏覽:18
安卓腳本一鍵打板 發布:2025-02-14 03:50:16 瀏覽:866
我的世界租賃伺服器會被封ip嗎 發布:2025-02-14 03:46:05 瀏覽:982
資料庫附 發布:2025-02-14 03:44:43 瀏覽:837
安卓系統解不了鎖怎麼辦 發布:2025-02-14 03:44:35 瀏覽:552
怎麼查詢信用卡密碼 發布:2025-02-14 03:42:04 瀏覽:318
studio外貿服裝直播腳本 發布:2025-02-14 03:34:16 瀏覽:453
python集合運算符 發布:2025-02-14 03:06:18 瀏覽:205
pic編譯軟體 發布:2025-02-14 03:01:04 瀏覽:984
反編譯在編譯 發布:2025-02-14 02:55:36 瀏覽:418