[英]Receive data using Python sockets UDP IPV6 for a specific source address and port
我正在嘗試使用 sockets 模塊(在 Windows 上運行)實現帶有 python 的服務器客戶端應用程序,這是我在互聯網上找到的最簡單的方法:
對於服務器:
import socket
UDP_IP = ''
UDP_PORT = 42557
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
a = 0
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print ("received message:", data)
print("addr is:", addr)
sock.close()
break
對於客戶:
import socket
UDP_IP = "::1"
UDP_PORT = 42557
MESSAGE = b"0000000EC4030004004000000000000000000000000000000000000000000000"
print ("UDP target IP:", UDP_IP)
print ("UDP target port:", UDP_PORT)
print ("message:", MESSAGE)
sock = socket.socket(socket.AF_INET6, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
sock.close()
好的,這工作正常。
問題是,我有一個包含多個包的 pcap 文件,如果我復制該文件,我可以看到所有使用線鯊通過接口(以太網卡)的包。
問題是,我如何使用 socket 模塊獲取這些包? 我嘗試了很多方法,但我什么都看不到(我是一個帶套接字的菜鳥)。
在 pcap 文件中,基本上信息如下:
源地址:FD53:7CB8:0383:0002:0000:0000:0.0.0.105 目的地址:FF14:0000:0000:0000:0000:0000:0.0.0.28 源端口:42994 目的端口:42512
有效載荷中有一些隨機數據。 即:0000000EC40300040040000000000000000000000000000000000000000000000
提前致謝。
我使用了 https://github.com/rigtorp/udpreplay並且它有效。 我使用了環回接口,因此 IP 地址不是問題,並且發送端口和接收端口必須不同。
我的順序是。
我首先建議:
嘗試回復這個簡單的消息。
./udpreplay -i lo -l hello_from_client.pcapng
我在網上找到的服務器/客戶端示例
客戶:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define DST_PORT 8080
#define SRC_PORT 8081
#define MAXLINE 1024
#define IP "127.0.0.1"
// Driver code
int main() {
struct sockaddr_in addr, srcaddr;
int sockfd;
char message[] = "Hello, World!";
char *hello = "Hello from clinet";
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket");
exit(1);
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(IP);
addr.sin_port = htons(DST_PORT);
memset(&srcaddr, 0, sizeof(srcaddr));
srcaddr.sin_family = AF_INET;
srcaddr.sin_addr.s_addr = htonl(INADDR_ANY);
srcaddr.sin_port = htons(SRC_PORT);
if (bind(sockfd, (struct sockaddr *) &srcaddr, sizeof(srcaddr)) < 0) {
perror("bind");
exit(1);
}
sendto(sockfd, (const char *)hello, strlen(hello),
MSG_CONFIRM, (const struct sockaddr *) &addr,
sizeof(addr));
printf("Hello message sent.\n");
int n, len;
char buffer[256];
n = recvfrom(sockfd, (char *)buffer, MAXLINE,
MSG_WAITALL, (struct sockaddr *) &addr,
&len);
buffer[n] = '\0';
printf("Server : %s\n", buffer);
close(sockfd);
return 0;
}
服務器:
// Server side implementation of UDP client-server model
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 8080
#define MAXLINE 1024
// Driver code
int main() {
int sockfd;
char buffer[MAXLINE];
char *hello = "Hello from server";
struct sockaddr_in servaddr, cliaddr;
// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
memset(&cliaddr, 0, sizeof(cliaddr));
// Filling server information
servaddr.sin_family = AF_INET; // IPv4
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);
// Bind the socket with the server address
if ( bind(sockfd, (const struct sockaddr *)&servaddr,
sizeof(servaddr)) < 0 )
{
perror("bind failed");
exit(EXIT_FAILURE);
}
int len, n;
len = sizeof(cliaddr); //len is value/resuslt
n = recvfrom(sockfd, (char *)buffer, MAXLINE,
MSG_WAITALL, ( struct sockaddr *) &cliaddr,
&len);
buffer[n] = '\0';
printf("Client : %s\n", buffer);
sendto(sockfd, (const char *)hello, strlen(hello),
MSG_CONFIRM, (const struct sockaddr *) &cliaddr,
len);
printf("Hello message sent.\n");
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.