简体   繁体   English

Python套接字客户端和服务器

[英]Python socket client and server

Well, I have a problem while doing socket programming in python. 好吧,我在用python进行套接字编程时遇到问题。

What I used to do with socket is that, first creating a client socket and sending a HTTP GET request to a server and receiving the response from that server. 我以前用套接字做的是,首先创建一个客户端套接字,然后向服务器发送HTTP GET请求,然后从该服务器接收响应。 It works fine. 工作正常。

Now my new idea is that I want to do it with two sockets, one send, one receive. 现在,我的新主意是我想使用两个套接字,一个发送,一个接收。

The sending socket is as usual, no surprise. 发送套接字照常进行,不足为奇。 The receive socket is a server which bind to the ipaddress used by sending socket. 接收套接字是绑定到发送套接字使用的ipaddress的服务器。

The problem is I cannot receive any response from remote server. 问题是我无法从远程服务器收到任何响应。 I used wireshark and see that remote server still sends back the data to the port which is used by sending socket. 我使用wireshark,发现远程服务器仍将数据发送回发送套接字所使用的端口。 However, I cannot bind my receiving socket to the that port, since it is being used. 但是,我无法将接收套接字绑定到该端口,因为正在使用该端口。

Is there anyway to achieve my scenario with two sockets? 无论如何,有两个插座可以实现我的方案吗? Do I have to bypass some system network protocol by using raw_socket? 我必须使用raw_socket绕过某些系统网络协议吗?

No, you cannot bind a receive socket to the same address as a distinct sending socket. 不可以,您不能将接收套接字绑定到与不同发送套接字相同的地址。

From the relevant Linux man page : 相关的Linux手册页中

only one IP socket may be bound to any given local (address, port) pair. 任何给定的本地(地址,端口)对都只能绑定一个IP套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM