Python套接字:如何获取客户端连接的地址?

时间:2015-09-12 17:02:15

标签: python sockets python-3.x

我有一些托管本地服务器的代码,当用户连接它时会向他们发送一些html代码,这样可以正常工作。 但我希望如此,如果他们连接到http://localhost:90/abc它会显示不同的东西。我怎样才能得到他们所连接的确切网址?

这是我的代码:

[(1, 2), (2, ave(prev, next_that_exists)), (3, ave(just_created, next_that exists), ...]

我尝试了import socket sock = socket.socket() sock.bind(('', 90)) sock.listen(5) print("Listening...") while True: client, address = sock.accept() print("Connection recieved: ", address) print(The exact url they connected to.) print() client.send(b'HTTP/1.0 200 OK\r\n') client.send(b"Content-Type: text/html\r\n\r\n") client.send(b'<html><body><h1>Hello, User!</body></html>') client.close() sock.close() ,但这得到了客户端ip,如果有类似的方式来获取它们连接到它的ip可能不会得到&#39; abc&#39;网址的一部分。

提前致谢。

3 个答案:

答案 0 :(得分:2)

Socket没有URL的概念,这是在套接字顶部上运行的HTTP协议的特定内容。因此,只有部分HTTP URL甚至可用于创建套接字。

$_POST['action']
  1. 指定URL使用的
  2. 内的内部协议
  3. 通过IP地址或主机名
  4. 指定远程主机
  5. 指定远程端口并且是可选的
  6. 指定网址
  7. 虽然TCP套接字实际上只知道第2和第3部分!这是因为TCP是一种非常基本的通信形式,HTTP在其上添加了许多功能,如请求和响应以及路径等。

    基本上,如果您正在实施HTTP服务器,那么知道/ abc部分是您的作业。看看this example。客户端实际上将/ abc部分发送到服务器,否则它无法知道请求的路径。

答案 1 :(得分:0)

当客户端连接到您的服务器时,它将发送:

List<Unit> units = JsonConvert.DeserializeObject<List<Unit>>(File.ReadAllText(jsonFile));

您的服务器需要解析class GameViewController: UIViewController { // Both of the Labels var timeLabel: UILabel? var scoreLabel: UILabel? override func viewDidLoad() { super.viewDidLoad() if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene { // Configure the view. let skView = self.view as! SKView skView.showsFPS = true skView.showsNodeCount = true /* Sprite Kit applies additional optimizations to improve rendering performance */ skView.ignoresSiblingOrder = true /* Set the scale mode to scale to fit the window */ scene.scaleMode = .AspectFill skView.presentScene(scene) //self.addScore(3) } // Creating the TimeLabel timeLabel = UILabel(frame: CGRectMake(0, 0, view.frame.width/4, view.frame.height/10)) timeLabel!.textAlignment = NSTextAlignment.Center timeLabel!.font = UIFont(name: "HelveticaNeue", size: 50) timeLabel!.textColor = UIColor.blackColor() timeLabel!.startCountdown(10) // Creating the ScoreLabel scoreLabel = UILabel(frame: CGRectMake(100, 0, view.frame.width/4, view.frame.height/10)) scoreLabel!.text = "0" scoreLabel!.textAlignment = NSTextAlignment.Center scoreLabel!.font = UIFont(name: "HelveticaNeue", size: 50) scoreLabel!.textColor = UIColor.blackColor() self.view.addSubview(timeLabel!) self.view.addSubview(scoreLabel!) } // This is run externally after the view is loaded func addScore (points: Int){ // This line is the one that gives me an error self.scoreLabel!.addScore(points) } 行并从中提取GET /abc HTTP/1.1 Host: localhost more headers... <blank line>

答案 2 :(得分:0)

您需要在代码中添加template <typename TWindow> class dialog_base { static INT_PTR CALLBACK dlg_proc_internal(HWND, UINT, WPARAM, LPARAM); protected: dialog_base(LPCWSTR templateName, HWND parent) { m_hwnd = CreateDialogParamW(hinstance_, templateName, parent, dlg_proc_internal, reinterpret_cast<LPARAM>(this)); } HWND m_hwnd; virtual INT_PTR CALLBACK dlg_proc(UINT, WPARAM, LPARAM) = 0; public: virtual ~dialog_base() { DestroyWindow(m_hwnd); } HWND GetHandle() const; void show() const; }; template <typename TWindow> INT_PTR dialog_base<TWindow>::dlg_proc_internal (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { dialog_base<TWindow>* pThis; if (msg == WM_INITDIALOG) { pThis = reinterpret_cast<dialog_base<TWindow>*>(lParam); // you CANT cancel dialog creation here when // using CreateDialog...(), only when using // DialogBox...()! So, no point in doing any // error checking on SetWindowLongPtr()... SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pThis)); // no point in trying to call pThis->dlg_proc() // here since it won't be dispatched to derived // classes anyway... return TRUE; // or FALSE, depending on your needs... } pThis = reinterpret_cast<dialog_base<TWindow>*>(GetWindowLongPtr(hWnd, GWLP_USERDATA)); if (!pThis) return FALSE; return pThis->dlg_proc(msg, wParam, lParam); }

尝试以下代码:

abc = client.recv(100).split()